Counting posts within categories
-
I’m continuing this post from a previous post so that I can focus on the main problem.
This code ouputs the number of posts in one or more categories:
function get_post_count($categories) { global $wpdb; $post_count = 0; foreach($categories as $cat) : $querystr = " SELECT count FROM $wpdb->term_taxonomy WHERE term_id = $cat"; $result = $wpdb->get_var($querystr); $post_count += $result; endforeach; return $post_count; }
Now I have to figure out how to subtract the number of posts which is not published.
I think I will needing to use a LEFT JOIN (or RIGHT JOIN).
But I’m not good using JOIN.I probably need to do something similar to this:
SELECT * 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->posts.post_status = 'publish' AND $wpdb->term_taxonomy.taxonomy = 'category' AND $wpdb->term_taxonomy.term_id = 4 ORDER BY RAND() LIMIT $num
Suggestions anyone? ??
Viewing 13 replies - 1 through 13 (of 13 total)
Viewing 13 replies - 1 through 13 (of 13 total)
- The topic ‘Counting posts within categories’ is closed to new replies.