0

I have a bilangual page where posts are sorted by categories and subcategories. When i check hoew many posts are actually displayed on a category page I get 312 in both languages. When I check the admin table for that same category it shows 291 posts for one language and 301 posts for the other language. Also when I count the posts with php in the template archive.php as shown below I get 291 / 301 instead of 312 posts.

$current_cat_id = get_query_var('cat');

$category = get_category($current_cat_id);

$count = $category->category_count;

What am I missing? Any suggestions / pointers appreciated.

UPDATE: On this page http://www.web-templates.nu/2008/09/21/get_category_count/index.html I found a function titled "get_category_count" and after placing it in functions.php and calling it with the category slug it first seemed to give the correct results. But the results actually still differ from what i see in the admin columns when filtering for the same category.

function wt_get_category_count($input = '') {
    global $wpdb;
    if($input == '')
    {
        $category = get_the_category();
        return $category[0]->category_count;
    }
    elseif(is_numeric($input))
    {
        $SQL = "SELECT $wpdb->term_taxonomy.count FROM $wpdb->terms, $wpdb->term_taxonomy WHERE $wpdb->terms.term_id=$wpdb->term_taxonomy.term_id AND $wpdb->term_taxonomy.term_id=$input";
        return $wpdb->get_var($SQL);
    }
    else
    {
        $SQL = "SELECT $wpdb->term_taxonomy.count FROM $wpdb->terms, $wpdb->term_taxonomy WHERE $wpdb->terms.term_id=$wpdb->term_taxonomy.term_id AND $wpdb->terms.slug='$input'";
    return $wpdb->get_var($SQL);
    }
}

What am i kissing? Thanks!!

1 Answer 1

1

I don't know if your issue is cached values at all, but get_category() does cache results, so it's a possibility.

Here are some other options:

$current_cat_id = get_query_var('cat');

$category = get_category($current_cat_id);

// One possibility:
$count_a = get_category_count( $category->slug );

// OR... another possibility:
$count_b = wp_count_terms( $category->slug );
2
  • a) Thank you for your suggestions! However, wp_count_terms gives a result that is totally off since it "Counts how many terms are in taxonomy." (developer.wordpress.org/reference/functions/wp_count_terms) whereas I am looking for the number of posts in a given category. Commented Jan 2, 2023 at 9:54
  • b) To my knowledge the function get_category_count is not a built in function, but it lead my in the right direction: On this page web-templates.nu/2008/09/21/get_category_count/index.html I found a function titled "get_category_count" and after placing it in functions.php it seems to give the correct results. So thank you very much for pointing me in the right direction! Commented Jan 2, 2023 at 9:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.