编辑:我在另一个帖子中详细阐述了这个主题:List categories by author ~ WITH COUNTER ~ (Wordpress) ......它可能更适合你。
当我在我的分类页面时,我希望读者看到谁在那里发帖,并按作者过滤。 (例如,Activism有127个帖子,37个是Adrian,12个是James,等等......)我已经尝试了一百万个东西,但没有什么能满足我的简单任务!那里有类似经历的人吗?
这就是我所拥有的:
<?php
$curauth = $wp_query->get_queried_object();
$auth_id = 1;
$cat_id = 2411;
$sql = "SELECT COUNT(*)
FROM $wpdb->posts p
JOIN $wpdb->term_relationships tr ON (p.ID = tr.object_id)
WHERE post_author = $auth_id
AND post_type = 'post'
AND post_status = 'publish'
AND post_date <= NOW()
AND tr.term_taxonomy_id = $cat_id
";
$post_count = $wpdb->get_var($sql);
echo "This Author has published $post_count posts in Activism<br />";
?>
编辑:
好的,这段代码有效!!! ......我很高兴,但仍然有点疑惑。为什么这样做,而上面的那个没有?
<?php
$auth_id = 1;
$cat_id = 2411;
$counter = "SELECT COUNT(*)
FROM $wpdb->posts
LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id)
LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
WHERE $wpdb->term_taxonomy.term_id = $cat_id
AND $wpdb->term_taxonomy.taxonomy = 'category'
AND $wpdb->posts.post_status = 'publish'
AND post_author = '$auth_id'
";
$user_count = $wpdb->get_var($counter);
echo "Author... has $user_count posts in Activism<br />";
?>