WordPress.org

Ideas

Default value for post meta

  1. Guido Scialfa
    Inactive

    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
    Member

    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: 5 months ago #

RSS feed for this topic

Reply

You must log in to post.

  • Rating

    12345
    1 Vote
  • Status

    This idea is under consideration