WordPress.org

Make WordPress Core

Opened 3 years ago

Last modified 5 weeks ago

#15675 new enhancement

Add a "parent" criteria to wp_get_object_terms()

Reported by: mikeschinkel Owned by:
Milestone: Future Release Priority: normal
Severity: normal Version: 3.1
Component: Taxonomy Keywords: needs-refresh has-patch
Focuses: Cc:

Description

Currently the core function wp_get_object_terms() does not allow the developer to filter by the parent field in the wp_term_taxonomy table.

I found this limitation while doing a code review on a client's code. I wanted to recommend elimination of direct SQL queries to minimize kittens crying but found that currently it's not possible to limit the results returned by the SQL query to only those taxonomy items with a given parent.

This patch adds a parent option for the $args parameter to wp_get_object_terms(). While I was in there I noticed several other things which I added to the patch:

1.) It updates the header documentation to be easier to scan and to fix some of the errors which appear have resulted in code updates without documentation updates.

2.) It adds DISTINCT for all but 'all_with_object_id'==$fields as per @scribu's suggestion on ticket #11003.

3.) It changes the code so that only one (1) SQL query is run when 'tt_ids'==$fields instead of running the standard query and then throwing away its results to run the query a second time. However, please check my logic to make sure I didn't introduce an unintentional error here.

4.) I added an options for 'fields' of both 'id=>name' and 'id=>term' because I've needed both of those use-cases several times in the past.

FYI, this patch should be considered a superset of #11003.

Attachments (1)

add-parent-arg-to-wp-get-object-terms.diff (6.0 KB) - added by mikeschinkel 3 years ago.
Adds a "parent" arg to wp_get_object_terms(), fixes the returning of duplicate terms and adds the options id=>name and id=>term for fields.

Download all attachments as: .zip

Change History (6)

mikeschinkel3 years ago

Adds a "parent" arg to wp_get_object_terms(), fixes the returning of duplicate terms and adds the options id=>name and id=>term for fields.

comment:1 mikeschinkel3 years ago

  • Cc mikeschinkel@… added

comment:2 scribu3 years ago

  • Keywords has-patch added

comment:3 nacin3 years ago

  • Milestone changed from Awaiting Review to Future Release

comment:4 wonderboymusic8 months ago

  • Keywords needs-refresh added

This no longer applies against trunk - there's a lot of churn in the docs and SQL

comment:5 landwire5 weeks ago

Just seen that wp_get_object_terms() still does not allow a parent parameter and came across this quite old ticket now, when searching for a solution of just returning a parent term. Are there any plans of implementing this?

Note: See TracTickets for help on using tickets.