logo
Ask your WordPress questions! Pay money and get answers fast! (more info)

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.

$10
get post count outside of workpress loop

I would like to display the post count of pages that have a particular taxonomy outside of the main page loop

Ideally this would be a function where I feed it the taxonomy slug and it returns the count of posts with that set.

For example on the page below I would like to be able to display a count of the employers in the menu like "members (1)" is. I also plan to use this in widgets

http://graduatejob.com/sector/audit/

npeplow | 06/06/12 at 1:57pm Edit
Tutorial: How to assign prize money


(6) Possible Answers Submitted...

See a chronological view of answers?

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

  • avatar
    Last edited:
    06/06/12
    2:04pm
    Jatin Soni says:

    try this

    <?php

    $postcount = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_status = 'publish'");

    echo $postcount;

    ?>

    Previous versions of this answer: 06/06/12 at 2:04pm

    • 06/06/12 2:04pm

      npeplow says:

      Hi Jatin

      How would that distinguish between the different taxonomy's?

    • 06/06/12 2:06pm

      Jatin Soni says:

      You mean count by taxonomy? Can you please describe in little detail.

  • avatar
    Last edited:
    06/06/12
    2:09pm
    John Cotton says:

    You're going to want to look at get_objects_in_term.


    function my_tax_count( $name ) {
    $ids = get_terms( $name, array( 'fields' => 'ids' ) );

    $objects = get_objects_in_term( $ids, $name );

    return count($objects);
    }


    It doesn't do precisely what you want, but if you pass it a list of the ids in the term, then you could do a count on the result.

    You could, of course, write a little custom SQL that does precisely what you want. Certainly you'd get far better performance, but with the downside that you risk things breaking if a future WP update changes the way things are structured in the database.

    Previous versions of this answer: 06/06/12 at 2:09pm

  • avatar
    Last edited:
    06/06/12
    2:07pm
    Luis Abarca says:

    This can work


    wp_list_categories('include=' . $your_cat_ID);

  • avatar
    Last edited:
    06/06/12
    2:12pm
    Francisco Javier Carazo Gil says:

    Well, if you want to do this only for a taxonomy:

    <?php

    $postcount = $wpdb->get_var(";

    SELECT SELECT COUNT(*)
    FROM $wpdb->posts p
    INNER JOIN $wpdb->term_relationships tr
    ON p.post_id = tr.object_id
    INNER JOIN $wpdb->terms t
    ON t.term_id = tr.term_id
    WHERE p.post_status = 'publish'
    "
    echo $postcount;

    ?>

  • avatar
    Last edited:
    06/06/12
    2:32pm
    Arnav Joy says:

    i am not sure what i am giving is what you want , but try it

    define it in functions.php
    <?php
    function returnCount($slug){
    global $wpdb;
    $row = $wpdb->get_row("SELECT tt.count FROM ".$wpdb->prefix."term_taxonomy tt LEFT JOIN ".$wpdb->prefix."terms t ON tt.term_id = t.term_id WHERE t.slug = '".$slug."'");
    return $row->count;
    }

    ?>

    and in the template use it as:-

    <?php
    $slug = 'uncategorized';
    $count = returnCount($slug);
    echo $count;
    ?>

  • avatar
    Last edited:
    06/07/12
    7:11am
    Rashad Aliyev says:

    $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
    echo 'Count :' .$term->count;

This question has expired.





Current status of this question: Community pot



Please log in to add additional discourse to this page.





Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.