[resolved] Post count by category (7 posts)

  1. Steven
    Posted 8 years ago #

    I want to display the number of posts within a specific category (or categories). In my template I have the following code:

    //Get categories from custom field on page, Categories are separated with ','
      $categories = explode(",", get_post_meta($post->ID, 'category_id', true));
      // Use only the first category in this page template
      $cat= 'cat=' . $categories[0];
      // Get current page. Is used for pagination.
      $page = (get_query_var('paged')) ? get_query_var('paged') : 1;
      //Get the posts for selecteed categories
      query_posts("$cat&paged=$page&orderby=title&order=ASC"); // &posts_per_page=-1&paged=$page'); // run the query
      //Get post count
      $post_count = get_post_count($categories, $wpdb);
      echo 'Antall artikler: ' . $post_count;

    In query.php file I have created the function get_post_count($categories, $wpdb).

    The following code returns a result:

    function get_post_count($categories, $wpdb) {
     $querystr = "
        SELECT count(*)
        FROM $wpdb->posts
        WHERE $wpdb->posts.post_status = 'publish'
      $result = $wpdb->get_var($querystr);
      return $result;

    But it returns too many posts.

    I've tried using the following code:

    SELECT count(*)
    FROM wp_posts
    LEFT JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)
    LEFT JOIN wp_term_taxonomy ON (wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id)
    WHERE wp_posts.post_status = 'publish'
    AND $wpdb->posts.post_type = 'post'
    AND wp_term_taxonomy.term_id IN (15)

    But this doesn't return anything at all. I've read a lot of different posts in here, but no one seems to have the answer.

    Has anyone managed to count the posts for a specific category?


  2. VelvetBlues.com
    Posted 8 years ago #

    Hmm. That's quite a bit of code.

    To simply display the category name and category count, just use:


    Of course, you'll probably need other parameters if you want to further customize the output. See http://codex.wordpress.org/Template_Tags/wp_list_categories

    Let me know if you have any questions. I've accomplished something similar on my blog. See http://www.velvetblues.com/category/web-development-blog/

  3. Steven
    Posted 8 years ago #

    Hi Velvet.
    I'm not counting the categories, but the number of posts within one or more categories.

    In the page template I have a meta_tag for which categories I would like display posts. I retrieve all posts for all categories defined in the meta_tag.

    Now I need to display how many posts that is.

  4. VelvetBlues.com
    Posted 8 years ago #

    I know that you aren't counting the categories. The parameter 'show_count' returns the number of posts in a category. Sorry if I was unclear, and please look at the documentation for more info.

    If you have the IDs for the categories, you simply pass them to the wp_list_categories 'include' parameter.

  5. Steven
    Posted 8 years ago #

    "wp_list_categories, displays a list of Categories as links"

    Any suggestions on how I can avoid listing categories?
    And just get the post count?

  6. Steven
    Posted 8 years ago #

    Ok, I'm almost there.
    This code ouputs the number of posts in the category.

    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;
      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.

  7. Steven
    Posted 8 years ago #

    I'm continuing this post here.

Topic Closed

This topic has been closed to new replies.

About this Topic