Default value for post meta

  1. Guido Scialfa

    Add fourth parameter to the get_post_meta for a default value to return in case the post meta is not set.
    Like the get_option do.

    For example:

    function get_post_meta( $post_id, $key = '', $single = false, $default = false ) {
    	$_meta = get_metadata('post', $post_id, $key, $single);
            return ( ! empty( $_meta ) ? $_meta : ( $default ? $default : $_meta ) );

    In this way we can prevent to write extra conditionals to check if the post meta value is not what we are looking for and then add a new value to our variable.

    Posted: 2 years ago #
  2. leec87

    This would be a fantastic addition. Currently, using bloated IF ELSE statements seems very inefficient (and results in calling the get_post_meta() function several times which can't be good)

    Posted: 1 year ago #

RSS feed for this topic


You must log in to post.

  • Rating

    1 Vote
  • Status

    This idea is under consideration