Skip to content

Instantly share code, notes, and snippets.

@Drivingralle
Last active January 13, 2020 01:12
Show Gist options
  • Save Drivingralle/544d06c2505e122bb26849a232d36014 to your computer and use it in GitHub Desktop.
Save Drivingralle/544d06c2505e122bb26849a232d36014 to your computer and use it in GitHub Desktop.
A filter for get_post_meta value
function dr_filter_post_meta_value( $input, int $post_id, string $meta_key, bool $single ) {
// Don't run in admin interface
if ( is_admin() ) {
return $input;
}
// Get list of filter
global $wp_current_filter;
$filter_key = count( $wp_current_filter ) - 2;
// Check if this filter is inside itself
if ( isset( $wp_current_filter[ $filter_key ] ) && 'get_post_metadata' === $wp_current_filter[ count( $wp_current_filter ) - 2 ] ) {
return $input;
}
// If my wanted meta field
if ( 'distance' === $meta_key ) {
// Geet the value, format and return it
return sprintf( __( '%s km', 'textdomain' ), get_post_meta( $post_id, 'distance', true ) );
}
// Reeturn unmodified value
return $input;
}
add_filter( 'get_post_metadata', 'dr_filter_post_meta_value', 10, 4 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment