admin管理员组文章数量:1122832
I'm trying to display the total number of comments for a users posts limited to a comment meta. I've tried using both get_comments()
and WP_Comment_Query()
but I get the same result of "1" for the comment count when it should be "3". Now in this example I can remove 'Count' => true
and just loop through the comments and that will return the correct amount of comments in a loop but the count still equals one.
They provide an example here on how to get the count but it doesn't work for me. Here's my function.
function get_unseen_comment_count() {
if ( !is_user_logged_in() )
return false;
$user_id = get_current_user_id();
$postids = array();
$posts = get_posts( array(
'numberposts' => -1,
'post_type' => 'fod_articles',
'author' => $user_id,
'post_status' => 'publish'
));
foreach( $posts as $post ) {
$postids[] = $post->ID;
}
$args = array(
'include' => $postids,
'meta_query' => array(
array(
'key' => 'viewed_status',
'value' => 'unseen'
)
),
'count' => true
);
// $comments_query = new WP_Comment_Query;
// $comments = $comments_query->query( $args );
$comments = get_comments($args);
echo '<span class="comment-count">';
echo $comments;
echo '</span>';
}
Now in the codex it doesn't say that get_comment()
supports meta query but you can see on line 181 here that it does. It doesn't however show support for include
but it definitely works when I test using a loop of the actual comments.
Some further testing when I remove the meta_query I do get the total comment count for all posts by the user so my media query seems to be the problem interfering with the count. I tried also just using meta_key
and meta_value
without a meta query but I get the same result..."1".
I'm trying to display the total number of comments for a users posts limited to a comment meta. I've tried using both get_comments()
and WP_Comment_Query()
but I get the same result of "1" for the comment count when it should be "3". Now in this example I can remove 'Count' => true
and just loop through the comments and that will return the correct amount of comments in a loop but the count still equals one.
They provide an example here on how to get the count but it doesn't work for me. Here's my function.
function get_unseen_comment_count() {
if ( !is_user_logged_in() )
return false;
$user_id = get_current_user_id();
$postids = array();
$posts = get_posts( array(
'numberposts' => -1,
'post_type' => 'fod_articles',
'author' => $user_id,
'post_status' => 'publish'
));
foreach( $posts as $post ) {
$postids[] = $post->ID;
}
$args = array(
'include' => $postids,
'meta_query' => array(
array(
'key' => 'viewed_status',
'value' => 'unseen'
)
),
'count' => true
);
// $comments_query = new WP_Comment_Query;
// $comments = $comments_query->query( $args );
$comments = get_comments($args);
echo '<span class="comment-count">';
echo $comments;
echo '</span>';
}
Now in the codex it doesn't say that get_comment()
supports meta query but you can see on line 181 here that it does. It doesn't however show support for include
but it definitely works when I test using a loop of the actual comments.
Some further testing when I remove the meta_query I do get the total comment count for all posts by the user so my media query seems to be the problem interfering with the count. I tried also just using meta_key
and meta_value
without a meta query but I get the same result..."1".
3 Answers
Reset to default 0Take a look at the arguments you're passing to get_comments() and compare them to the documentation on the WordPress Codex.
Your example -
$args = array(
'include' => $postids,
'meta_query' => array(
array(
'key' => 'viewed_status',
'value' => 'unseen'
)
),
'count' => true
);
The codex example -
$defaults = array(
'author_email' => '',
'ID' => '',
'karma' => '',
'number' => '',
'offset' => '',
'orderby' => '',
'order' => 'DESC',
'parent' => '',
'post_id' => '',
'post_author' => '',
'post_name' => '',
'post_parent' => '',
'post_status' => '',
'post_type' => '',
'status' => '',
'type' => '',
'user_id' => '',
'search' => '',
'count' => false
);
- You're passing a multidimensional array when the docs clearly indicate a single dimensional array.
- You're also passing an array of 'Post IDs' when the docs clearly indicate that an integer is expected.
My solution is just to use count()
to determine the number of items in the array returned by $comments
which does the trick but I'm going to leave this unchecked as that doesn't explain why the function method isn't working.
I just tested your code as-is on my site (I manually added comment meta values to 3 comments) and it worked fine.
The only thing I did was comment out 'post_type' => 'fod_articles',
since I don't have that post type. Is it supposed to be fod_articles
or food_articles
?
Edit:
I tested again, this time with a custom post type, and it worked after making this change:
$args = array(
'post__in' => $postids,
'meta_query' => array(
array(
'key' => 'viewed_status',
'value' => 'unseen'
)
),
'count' => true
);
Basically change include
to post__in
本文标签: Show count of all comments by metavalue from a users posts
版权声明:本文标题:Show count of all comments by meta_value from a users posts 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736290905a1928611.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论