wp query - Get amount of CPT with a certain custom field value

admin2025-06-05  6

What I try to achieve:

Get the amount of CPT-posts with a certain condition.

My fields

CPT - 'usr_msg' FIELD NAME - 'posted_user_id'

My attemp

add_shortcode('count_usr_msg','count_usr_msg');
function count_usr_msg($atts){
    $usr_id = $atts['usr_id'];
    $args = array(
        'post_type'              => array( 'usr_msg' ),
        'meta_query'             => array(
            'relation' => 'AND',
            array(
                'key'     => 'posted_user_id',
                'value'   => $usr_id,
                'compare' => '=',
            )
        )
    );

    $usr_msg = new WP_Query( $args );

    if ($usr_msg->have_posts()) {
        $count_posts = wp_count_posts()->publish;
        return $count_posts;
    }else{
        return 0;
    }
}

The problem

If I post a user_id that has 0 posts, it returns 0, but when I post a user_id that has 1 or more it always returns 3.

What I try to achieve:

Get the amount of CPT-posts with a certain condition.

My fields

CPT - 'usr_msg' FIELD NAME - 'posted_user_id'

My attemp

add_shortcode('count_usr_msg','count_usr_msg');
function count_usr_msg($atts){
    $usr_id = $atts['usr_id'];
    $args = array(
        'post_type'              => array( 'usr_msg' ),
        'meta_query'             => array(
            'relation' => 'AND',
            array(
                'key'     => 'posted_user_id',
                'value'   => $usr_id,
                'compare' => '=',
            )
        )
    );

    $usr_msg = new WP_Query( $args );

    if ($usr_msg->have_posts()) {
        $count_posts = wp_count_posts()->publish;
        return $count_posts;
    }else{
        return 0;
    }
}

The problem

If I post a user_id that has 0 posts, it returns 0, but when I post a user_id that has 1 or more it always returns 3.

Share Improve this question edited Jan 1, 2019 at 12:13 Krzysiek Dróżdż 25.6k9 gold badges53 silver badges74 bronze badges asked Jan 1, 2019 at 12:01 Chris WieseChris Wiese 133 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Well, it returns wrong number, because you've coded it exactly in that way ;)

Your WP_Query is correct. So when there are no posts matching your conditions, then you get 0.

But when there are such posts, then... you ignore the query completely and return wp_count_posts as result... But...

This function returns an object, whose properties are the count of each post status of a post type. It does NOT count posts in given query - and TBH - in your case it couldn't do that (how should this function now what query to count posts of?)

So here's your code in correct form:

add_shortcode('count_usr_msg','count_usr_msg');
function count_usr_msg($atts){
    $usr_id = $atts['usr_id'];
    $args = array(
        'post_type'              => 'usr_msg',
        'meta_query'             => array(
            array(
                'key'     => 'posted_user_id',
                'value'   => $usr_id,
                'compare' => '=',
            )
        )
    );

    $usr_msg = new WP_Query( $args );

    return $usr_msg->found_posts;
}

Instead of checking some conditions, we just use found_posts, which contains:

The total number of posts found matching the current query parameters

and return it as a value.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749053877a315961.html

最新回复(0)