$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>wp query - Get amount of CPT with a certain custom field value|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin10PV0评论

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.

Post a comment

comment list (0)

  1. No comments so far