0

编辑:我在另一个线程中详细阐述了这个主题:按作者列出类别〜WITH COUNTER〜(Wordpress) ......它可能更适合你。

当我在我的类别页面中时,我希望读者看到谁在那里发帖,并按作者过滤。(例如,Activism 有 127 个帖子,Adrian 有 37 个帖子,James 有 12 个帖子,等等……)我已经尝试了上百万个事情,但没有什么能满足我的简单任务!有类似经历的人吗?

这就是我所拥有的:

<?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 />";

?>

编辑:

好的,这段代码有效!!!……我很高兴,但还是有点疑惑。为什么这行得通,而上面的 pne 不行?

<?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 />";

?>
4

0 回答 0