admin管理员组文章数量:1130349
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 badges1 Answer
Reset to default 1Well, 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.
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 badges1 Answer
Reset to default 1Well, 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.
本文标签: wp queryGet amount of CPT with a certain custom field value
版权声明:本文标题:wp query - Get amount of CPT with a certain custom field value 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749053853a2309036.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论