WordPress.org

Make WordPress Core

Opened 3 years ago

Closed 21 months ago

Last modified 21 months ago

#31105 closed enhancement (duplicate)

Abstract get_terms() arguments for use in wp_get_object_terms()

Reported by: boonebgorges Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: Taxonomy Keywords: needs-patch
Focuses: Cc:

Description

While get_terms() and wp_get_object_terms() have different use cases, there's a lot of overlap in terms of how one might want to filter results by the various properties of the matched terms. There's no reason why wp_get_object_terms() shouldn't support many of the term-filtering arguments of get_terms(): exclude, exclude_tree, include, name, slug, parent, child_of, namelike, descriptionlike, search, hierarchical, and perhaps others. See #18106, #15675, etc.

Obviously, reproducing all of the argument parsing and SQL generation of get_terms() inside of wp_get_object_terms() is not ideal. It'd be nice of some of this was abstracted out - into a helper class? I dunno - so that it could be shared by the two functions, and perhaps elsewhere.

Change History (3)

#1 @boonebgorges
2 years ago

WP_Term_Query should make this easier. [37572].

#2 @boonebgorges
21 months ago

  • Milestone Future Release deleted
  • Resolution set to duplicate
  • Status changed from new to closed

Closing in favor of #37198.

#3 @boonebgorges
21 months ago

In 38667:

Taxonomy: Use WP_Term_Query when querying for object terms.

The new 'object_ids' parameter for WP_Term_Query allows queries for
terms that "belong to" a given object. This change makes it possible
to use WP_Term_Query inside of wp_get_object_terms(), rather than
assembling a SQL query.

The refactor has a couple of benefits:

  • Less redundancy.
  • Better consistency in accepted arguments between the term query functions. See #31105.
  • Less redundancy.
  • Object term queries are now cached. The get_object_term_cache() cache remains, and will be a somewhat less fragile secondary cache in front of the query cache (which is subject to frequent invalidation).
  • Less redundancy.

A small breaking change: Previously, if a non-hierarchical taxonomy had
terms that had a non-zero 'parent' (perhaps because of a direct SQL
query), wp_get_object_terms() would respect the 'parent' argument.
This is in contrast to WP_Term_Query and get_terms(), which have
always rejected 'parent' queries for non-hierarchical taxonomies. For
consistency, the behavior of get_terms() is being applied across the
board: passing 'parent' for a non-hierarchical taxonomy will result in
an empty result set (since the cached taxonomy hierarchy will be empty).

Props flixos90, boonebgorges.
See #37198.

Note: See TracTickets for help on using tickets.