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 | 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)
#2
@
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
@
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 );
@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.
It may be that WP would have to do the same thing internally, which would be just as inefficient :)