Support » Fixing WordPress » Calculate number of posts with a specific custom field and a specific category

  • Resolved csseur3



    i have many posts with a custom field called “test”, i want to calculate the total number of this posts and show it on my pages, how to do that?


Viewing 9 replies - 1 through 9 (of 9 total)
  • Could use the wpdb class for that:

    $test_count = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->postmeta WHERE meta_key = 'test';");
    echo '<p>test count is ' . $test_count . '</p>';


    i try to limit this at a category but nothing is show:

    $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->postmeta WHERE term_ID='3' AND meta_key = 'actu';");

    what is wrong?


    oups, i think have missed one thing, but when i try the new query

    $test_count = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->postmeta WHERE meta_key = 'actu' AND FROM $wpdb->terms WHERE term_ID=133");

    nothing again 🙁

    You will want to consider the information described here:

    Note: also changed title to reflect you want custom field and category test

    i try

    $test_count = $wpdb->get_var(“SELECT COUNT(*) FROM $wpdb->posts
    LEFT JOIN $wpdb->postmeta ON($wpdb->posts.ID = $wpdb->postmeta.post_id)
    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 = 133
    AND $wpdb->term_taxonomy.taxonomy = ‘category’
    AND $wpdb->posts.post_status = ‘publish’
    AND $wpdb->postmeta.meta_key = ‘actu’
    ORDER BY $wpdb->postmeta.meta_value ASC”);
    echo ” . $test_count . ”;

    but the categori with ID 133 have many sub-categories and the count works only on the sub-category..; this is normal? i have many subcategories and i search a more friendly method to do that please.


    Use get_categories to get an array of child categories and then include those categories in your query.

    i have this code:

          $cat = get_query_var('cat');
          if ($categories) {
            foreach($categories as $term) {
              echo '' . $term->cat_ID .' ';

    how to include that in the sql query?


    If you want to know the number of posts in categories 1,2, and 3 that have the meta_key of “actu”, then you can do the following:

    $posts_with_actu = get_posts(array(
            'category__in' => array(1,2,3),
            'meta_key' => 'actu',
            'showposts' => -1,
    printf('I have %d posts with "actu"!', count($posts_with_actu));

    Thanks filosofo!

    But with “SELECT SUM” operation, use “WHERE $wpdb->term_taxonomy.term_id” with multiple categories is always impossible.

    Thanks for solution for “SELECT COUNT” operation, i read the forum for a solution for “SELECT SUM” 🙂


Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Calculate number of posts with a specific custom field and a specific category’ is closed to new replies.