Make WordPress Core

Opened 8 years ago

Last modified 2 years ago

#40696 accepted enhancement

no chance to control wp_count_terms ()

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

Description

Hey!

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,
Christian

Attachments (4)

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

Download all attachments as: .zip

Change History (21)

#1 @SergeyBiryukov
5 years ago

#49411 was marked as a duplicate.

#2 @SergeyBiryukov
5 years ago

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

#3 @SergeyBiryukov
5 years ago

  • Keywords good-first-bug added

@jignashu
5 years ago

Added filter to modify wp_count_terms arguments.

#4 @jignashu
5 years ago

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

By mistake changed to invalid.

Last edited 5 years ago by jignashu (previous) (diff)

#5 @jignashu
5 years ago

  • Resolution invalid deleted
  • Status changed from closed to reopened

#6 @amitkumarsingh
5 years 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.

Checkout the diff, I just uploaded below.

Last edited 5 years ago by amitkumarsingh (previous) (diff)

#7 @jaydeeptrivedi404
5 years ago

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

#8 @jignashu
5 years 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
5 years ago

Hello.
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.

@jignashu
5 years ago

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

#10 @ddu66
5 years 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.

#11 @ddu66
4 years ago

Hello.
4 years later, the need for a filter call before returning is still present.
Would it be possible to take this request into account ?
Filtering the args could by interesting but doesn't give the hability to return terms that are not present in the database (and this is exactly the need).
Regards.

#13 @azouamauriac
3 years ago

  • Keywords needs-refresh added

#14 @audrasjb
2 years ago

  • Milestone changed from Future Release to 6.1
  • Owner set to audrasjb
  • Status changed from reopened to accepted

Hello,

Sorry for the long delay and thank you for the patch @ddu66.
Moving for 6.1 consideration and self-assigning for review.

This ticket was mentioned in PR #2875 on WordPress/wordpress-develop by audrasjb.


2 years ago
#15

  • Keywords needs-refresh removed

#16 follow-up: @peterwilsoncc
2 years ago

I'd like to wrap up #40351 before putting this in.

The intention is to improve the performance of term counts significantly so there is a risk adding a filter would block that in an effort to maintain backward compatibiltiy.

#17 in reply to: ↑ 16 @desrosj
2 years ago

  • Milestone changed from 6.1 to Future Release

Replying to peterwilsoncc:

I'd like to wrap up #40351 before putting this in.

The intention is to improve the performance of term counts significantly so there is a risk adding a filter would block that in an effort to maintain backward compatibiltiy.

I agree with this. Since #40351 is still marked Future Release, let's also move this one there.

Note: See TracTickets for help on using tickets.