Using WordPress ‘default_{$meta_type}_metadata’ PHP filter

The default_{$meta_type}_metadata WordPress PHP filter allows you to modify the default metadata value for a specified meta key and object, depending on the meta object type (post, comment, term, user, or any other type with an associated meta table).

On this pageJump to a section

Usage

add_filter('default_post_metadata', 'my_custom_default_metadata', 10, 5);

function my_custom_default_metadata($value, $object_id, $meta_key, $single, $meta_type) {
  // your custom code here
  return $value;
}

Parameters

  • $value (mixed): The value to return, either a single metadata value or an array of values depending on the value of $single.
  • $object_id (int): ID of the object metadata is for.
  • $meta_key (string): Metadata key.
  • $single (bool): Whether to return only the first value of the specified $meta_key.
  • $meta_type (string): Type of object metadata is for. Accepts ‘post’, ‘comment’, ‘term’, ‘user’, or any other object type with an associated meta table.

More information

See WordPress Developer Resources: default_{$meta_type}_metadata

Examples

Set default post metadata

Set a default value for a custom post meta key called ‘post_views’.

add_filter('default_post_metadata', 'set_default_post_views', 10, 5);

function set_default_post_views($value, $object_id, $meta_key, $single, $meta_type) {
  if ($meta_key === 'post_views') {
    $value = 0;
  }
  return $value;
}

Set default user metadata

Set a default value for a custom user meta key called ‘user_bio’.

add_filter('default_user_metadata', 'set_default_user_bio', 10, 5);

function set_default_user_bio($value, $object_id, $meta_key, $single, $meta_type) {
  if ($meta_key === 'user_bio') {
    $value = 'New user, no bio yet!';
  }
  return $value;
}

Set default term metadata

Set a default value for a custom term meta key called ‘term_icon’.

add_filter('default_term_metadata', 'set_default_term_icon', 10, 5);

function set_default_term_icon($value, $object_id, $meta_key, $single, $meta_type) {
  if ($meta_key === 'term_icon') {
    $value = 'default-icon';
  }
  return $value;
}

Set default comment metadata

Set a default value for a custom comment meta key called ‘comment_rating’.

add_filter('default_comment_metadata', 'set_default_comment_rating', 10, 5);

function set_default_comment_rating($value, $object_id, $meta_key, $single, $meta_type) {
  if ($meta_key === 'comment_rating') {
    $value = 0;
  }
  return $value;
}

Set default metadata for custom object type

Set a default value for a custom object type meta key called ‘custom_object_color’.

add_filter('default_custom_object_metadata', 'set_default_custom_object_color', 10, 5);

function set_default_custom_object_color($value, $object_id, $meta_key, $single, $meta_type) {
  if ($meta_key === 'custom_object_color') {
$value = 'blue';
}
return $value;
}

With these examples, you can set default metadata values for various object types in WordPress. Adjust the filter names, meta keys, and default values according to your specific use case.

Leave a Comment

Your email address will not be published. Required fields are marked *