Make WordPress Core

Opened 8 years ago

Last modified 4 years ago

#39372 new feature request

Allow count for wp_terms query to count other post statuses as well as published posts

Reported by: roperjonathan's profile roperjonathan Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 4.7
Component: Taxonomy Keywords: reporter-feedback
Focuses: performance Cc:

Description

While it is great to use the wp_terms query to get the terms, when counting the number of posts assigned to each of these terms, it would be great if there was a way to be able to include the number of posts that have other post statuses and not just count the number of posts that have been published.

At the moment if I want to do this, I have to return all of the terms from a taxonomy and then go through each one and count how many posts are associated with each term for both published posts and draft posts which is a lot more inefficient.

Change History (7)

#1 @boonebgorges
8 years ago

  • Keywords reporter-feedback added

@roperjonathan Thanks for the ticket, and welcome to WordPress Trac!

Can you give an example of the syntax you'd like to use in your plugin or theme? I'm not sure I understand the use case.

At the moment if I want to do this, I have to return all of the terms from a taxonomy and then go through each one and count how many posts are associated with each term for both published posts and draft posts which is a lot more inefficient.

It may be that WP would have to do the same thing internally, which would be just as inefficient :)

#2 @roperjonathan
8 years ago

So I recently had an example where I needed to count the number of posts associated with each term for a taxonomy but both for posts that are published and that are drafts.

In order to do this I did the following:

<?php

if(!empty($slug)) {
                $terms_args = array(
                        'orderby'    => 'name',
                        'order'      => 'ASC',
                        'hide_empty' => false,
                        'fields'     => 'all',
                        'pad_counts' => true,
                );
                $terms = get_terms($slug, $terms_args);
                return $terms;
}

and then once I got all of the terms for a particular taxonomy, then do a WP Query and then if the count returns 1 or more than show the option in a dropdownlist with the count:

<?php
if ( ! empty( $terms ) ) {
        foreach ( $terms as $term ) { 
                $st = $wpdb->prepare("SELECT COUNT(*) FROM $wpdb->posts p
                INNER JOIN $wpdb->term_relationships tr
                ON (p.ID = tr.object_id)
                INNER JOIN $wpdb->term_taxonomy tt
                ON (tr.term_taxonomy_id = tt.term_taxonomy_id)
                WHERE
                tt.term_id = %d
                AND (p.post_status = 'publish'
                OR p.post_status = 'in-progress');",
                $term->term_id);
                
                $results = $wpdb->get_var($st);

                if($results){
                $output .= '<option value="' . $term->slug . '">' . $term->name . ' (' . $results . ') </option>';
                }
        }
}

I would have liked it so that there would be an option in the $terms_args to return and count terms that have posts of a certain post status associated to them. So for example, like with the post_status option you can use with the get_posts function. This would then mean I wouldn't have needed the second part of the code.

#3 @dd32
8 years ago

For reference; a hacky solution I've used elsewhere, such as on the WordPress.org plugin directory, is to include a non-exitent post-type into the taxonomy post_type list.
It causes WordPress to skip only counting post_status = 'publish' and instead just counting the number of objects assigned to it, not looking at the post table at all.

register_taxonomy( 'plugin_contributors', array( 'plugin', 'force-count-to-include-all-post_status' ), $args );

https://meta.trac.wordpress.org/browser/sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/class-plugin-directory.php?rev=4520&marks=201,217#L190

#4 @SergeyBiryukov
8 years ago

  • Component changed from General to Taxonomy

Related: #32942, #38280.

This ticket was mentioned in Slack in #core by davecpage. View the logs.


6 years ago

#7 @adamsilverstein
4 years ago

Hi @roperjonathan - Would this issue be resolved by adding the proposed filter in #38843?

Note: See TracTickets for help on using tickets.