Make WordPress Core

Opened 4 years ago

Last modified 11 months ago

#40696 reopened enhancement

no chance to control wp_count_terms ()

Reported by: krizkroz Owned by:
Milestone: Future Release Priority: normal
Severity: normal Version: 4.7.4
Component: Taxonomy Keywords: good-first-bug has-patch
Focuses: Cc:



I'm wondering if i have no chance to filter the resulting number of wp_count_terms ().
Theres no filter available there.

wp_count_terms () calls get_terms () but it returns the result of $term_query before ("Count queries are not filtered, for legacy reasons.") the "get_terms" filter is appliable. So i see no chance to control result of wp_count_terms () without (heavy) diggin in to WP_Term_Query().

Is there a chance for a filter before the function returns in this case?

thank you a lot,

Attachments (4)

40696.diff (497 bytes) - added by jignashu 11 months ago.
Added filter to modify wp_count_terms arguments.
40696.2.diff (674 bytes) - added by amitkumarsingh 11 months ago.
40696.3.diff (775 bytes) - added by jaydeeptrivedi404 11 months ago.
40696.4.diff (614 bytes) - added by jignashu 11 months ago.
Changed the name of the filter as well as added block comments for that filter.

Download all attachments as: .zip

Change History (14)

#1 @SergeyBiryukov
12 months ago

#49411 was marked as a duplicate.

#2 @SergeyBiryukov
12 months ago

  • Keywords needs-patch added
  • Milestone changed from Awaiting Review to Future Release
  • Type changed from feature request to enhancement

#3 @SergeyBiryukov
12 months ago

  • Keywords good-first-bug added

11 months ago

Added filter to modify wp_count_terms arguments.

#4 @jignashu
11 months ago

  • Keywords needs-patch removed
  • Resolution set to invalid
  • Status changed from new to closed

By mistake changed to invalid.

Last edited 11 months ago by jignashu (previous) (diff)

#5 @jignashu
11 months ago

  • Resolution invalid deleted
  • Status changed from closed to reopened

#6 @amitkumarsingh
11 months ago

  • Keywords has-patch added

Added a filter to modify the arguments of wp_count_terms. Similar to above solution but I think having a filter name like wp_count_terms_args would make more sense.

Version 0, edited 11 months ago by amitkumarsingh (next)

#7 @jaydeeptrivedi404
11 months ago

@amitkumarsingh @jignashu Wont it be better to have a filter directly for the result?

#8 @jignashu
11 months ago

@jaydeeptrivedi404 yes, it is good to have it on the result as well but it also required before calling the get_terms() to modify the arguments of the function.

#9 @ddu66
11 months ago

I don't understand some of your proposals regarding of initial request.
As I explain in ticket #49411 (duplicate), the need is to filter result exactly the same way wp_count_posts (wp-includes/posts.php) does.
wp_count_terms function should then terminate with:

return apply_filters('wp_count_terms', get_terms($args), $taxonomy, $args);

Thank you.

11 months ago

Changed the name of the filter as well as added block comments for that filter.

#10 @ddu66
11 months ago

Filtering the result give user more control and make wp_count_terms mostly coherent with wp_count_posts function.
In another hand, I think that filtering args should be done by get_terms instead of wp_count_terms.

Note: See TracTickets for help on using tickets.