default_(meta_type)_metadatafilter-hookWP 5.5.0

Allows changing the default value of a post, taxonomy term, user, or comment metadata field.

The dynamic part of the hook name, (meta_type), is the name of the metadata object type. Therefore, this hook can have the following names:

Usage

add_filter( 'default_(meta_type)_metadata', 'wp_kama_default_meta_type_metadata_filter', 10, 5 );

/**
 * Function for `default_(meta_type)_metadata` filter-hook.
 * 
 * @param mixed  $value     The value to return, either a single metadata value or an array of
 *                          values depending on the value of `$single`.
 * @param int    $object_id ID of the object metadata is for.
 * @param string $meta_key  Metadata key.
 * @param bool   $single    Whether to return only the first value of the specified `$meta_key`.
 * @param string $meta_type Type of object metadata is for. Accepts 'blog', 'post', 'comment',
 *                          'term', 'user', or any other object type with an associated meta table.
 *
 * @return mixed
 */
function wp_kama_default_meta_type_metadata_filter( $value, $object_id, $meta_key, $single, $meta_type ){
	// filter...
	return $value;
}
$value(mixed)
The default value to return. It can be a string or an array, depending on the $single parameter.
$object_id(int)
ID of the metadata object: post, term, comment, or user ID.
$meta_key(string)
Metadata field name (metadata key).
$single(true|false)
How the values were requested. See the corresponding parameter of get_post_meta().
$meta_type(string)
The type of object the metadata field belongs to. It can be post, comment, term, user, or any other type if you created fields for a custom table.

Examples

#1 Set a default view count when a post has no views yet

Suppose the number of post views is stored in the views metadata field, and when a post has no views yet, we want to display some number instead of 0 to encourage readers' interest in the article.

At the same time, we do not want to actually change the views field value: if views exist, they should be displayed, but if there are none, a value such as 10 should be shown.

We could use a random number so that all posts do not have the same number of views, but then that number would keep changing, which is also undesirable. Instead, let us take the last two digits of the post ID and use them as the view count.

# Set a default view count when a post has no views yet
add_filter( 'default_post_metadata', 'default_post_views_meta_value', 10, 5 );

function default_post_views_meta_value( $value, $object_id, $meta_key, $single, $meta_type ){

	if( 'views' === $meta_key && 'post' === $meta_type ){

		// if 00 show 10
		$value = (int) substr( $object_id, -2 ) ?: 10;
	}

	return $value;
}

#2 Substitute a default image

This code adds support for a default image for fundraising campaigns in the Leyka plugin when the user has not uploaded one.

It uses ACF, where an administrator specifies default images for each campaign type (birthday, sport, and so on).

The code is taken out of context and demonstrates only the filter's capabilities.

  1. The default_post_metadata filter

    • Runs when post metadata is retrieved.
    • Checks that _thumbnail_id (the image ID) is being requested.
    • After confirming that the post belongs to the leyka_campaign post type, retrieves its category (campaign type).
    • Returns the ID of the default image corresponding to that category.
  2. The get_campaign_default_image_id() function
    • Retrieves the list of default images configured by the administrator in ACF (the global_default_campaign_images field).
    • If an image is set for the required type (birthday, masterclass, sport, or other), returns its ID.
    • Otherwise, returns 0.

Thus, if a campaign has no image of its own, the default image specified in the settings will be used.

add_filter( "default_post_metadata", function ( $value, $post_id, $meta_key, $single ) {
	if ( $meta_key !== '_thumbnail_id' ) {
		return $value;
	}

	if ( 'leyka_campaign' !== get_post_type( $post_id ) ) {
		return $value;
	}

	$type = get_campaign_type( 'key', $post_id );

	return get_campaign_default_image_id( $type );
}, 10, 4 );

/**
 * Gets the ID of the campaign's default image.
 * Default photos are configured in the admin settings.
 *
 * @param string $type Image type (birthday, masterclass, sport, or other).
 *
 * @return int
 */
function get_campaign_default_image_id( string $type ): int {
	$image_ids = get_field( 'global_default_campaign_images', 'option' );

	if ( ! empty( $image_ids[ $type ] ) ) {
		return absint( $image_ids[ $type ] );
	}

	return 0;
}

Changelog

Since 5.5.0 Introduced.

Where the hook is called

get_metadata_default()
default_(meta_type)_metadata
wp-includes/meta.php 743
$value = apply_filters( "default_{$meta_type}_metadata", $value, $object_id, $meta_key, $single, $meta_type );

Where the hook is used in WordPress

wp-includes/meta.php 1551
add_filter( "default_{$object_type}_metadata", 'filter_default_metadata', 10, 5 );