WordPress.org

Ready to get started?Download WordPress

Forums

get_post_meta to check for 0 meta value (3 posts)

  1. hitcher
    Member
    Posted 2 years ago #

    Hi,

    I'd like to check for 0 meta value in post_meta table. I'm trying something like this but no luck so far:

    $meta_values = get_post_meta($post->ID, $meta_key, $single = true);
                    foreach($meta_values as $value){
                        if( $value==0){
                            $num_ratings++;
                        }
                        }

    Reason for this is that I only need to count number of non 0 meta_value records per post ID per given meta_key.

  2. Frumph
    Member
    Posted 2 years ago #

    setting $single to true returns the value as a string, not an array

    $meta_values = get_post_meta($post->ID, 'keyname', false);

    will return as array.

    You should also check meta_values after the fact with is_array()

    if (is_array($meta_values)) {

  3. hitcher
    Member
    Posted 2 years ago #

    thank you got it sorted. it was custom post type $meta_key problem.

Topic Closed

This topic has been closed to new replies.

About this Topic