2010-04-26 53 views
0

我想顯示用戶從特定類別的帖子數。目前,我只能查詢所有帖子。我的代碼是這樣的用戶從特定類別的帖子數[Wordpress]

<?php $userpost_count = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_status = 'publish' AND post_type ='post' AND post_author = '".$curauth->ID."'");?> 
<?php echo "<span>Total post: </b></span>".$userpost_count.""?> 

我知道,我需要把兩個表,該表交表,term_relationships,但我不知道如何得到它。請善意幫助我。謝謝。

回答

1

下面用你的CATEGORY_ID和嘗試這個辦法:

<?php $userpost_count = $wpdb->get_var("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 = CATEGORY_ID 
AND $wpdb->term_taxonomy.taxonomy = 'category' 
AND $wpdb->posts.post_status = 'publish' 
AND post_author = '".$curauth->ID."'");?> 
+0

完美。真的感謝。它的工作 – morningglory 2010-04-26 12:58:22

相關問題