Skip to content

Add support for flushing term cache when objects terms are modified - #28

Closed
MosheGross wants to merge 3 commits into
acato-plugins:masterfrom
MosheGross:master
Closed

MosheGross wants to merge 3 commits into
acato-plugins:masterfrom
MosheGross:master

Conversation

@MosheGross

Copy link
Copy Markdown
Contributor

As of now, lets say i query
/wp-json/wp/v2/categories
I get back the count for each category (and by default it also hides empty categories).

If I add a post or edit a posts category, the counts should change, but they don't.

So I added function to hook into set_object_terms.

I added the filter wp_rest_cache/flush_on_set_terms and defaulted it to false for performance, as the function is quite heavy, it has to do the following

  • Get term ids from taxonomy ids
  • Get all term ids for object
  • Get all parent term ids if the taxonomy is hierarchical
  • Loop over each affected term (including ancestors) and clear related caches

@MosheGross
MosheGross marked this pull request as draft January 28, 2026 13:33
@MosheGross
MosheGross marked this pull request as ready for review January 28, 2026 13:33
@MosheGross

Copy link
Copy Markdown
Contributor Author

@richardkorthuis can you have a look pls

@richardkorthuis

richardkorthuis commented Feb 25, 2026 •

Copy link
Copy Markdown
Collaborator

Hi @MosheGross

Sorry for the late reply, but I finally had time to check your PR.

I am going to close your PR, not because I don't like it, but because I have implemented your changes with some small improvements, for instance some code that didn't completely adhere to the WP Coding Standards and also I made the wp_rest_cache/flush_on_set_terms to default to true. We just released a new version of our plugin with this improvement in it.

Thank you for your contribution!

@MosheGross

Copy link
Copy Markdown
Contributor Author

@richardkorthuis amazing, thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants