Make WordPress Core

Opened 13 years ago

Closed 9 years ago

#15675 closed enhancement (fixed)

Add a "parent" criteria to wp_get_object_terms()

Reported by: mikeschinkel's profile mikeschinkel Owned by:
Milestone: 4.2 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 13 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 (9)

@mikeschinkel
13 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.

#1 @mikeschinkel
13 years ago

  • Cc mikeschinkel@… added

#2 @scribu
13 years ago

  • Keywords has-patch added

#3 @nacin
13 years ago

  • Milestone changed from Awaiting Review to Future Release

#4 @wonderboymusic
11 years ago

  • Keywords needs-refresh added

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

#5 @landwire
10 years 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?

#6 @Mte90
10 years ago

Any news about this patch?

#7 @boonebgorges
9 years ago

  • Milestone changed from Future Release to 4.2

Regarding the specific request here ('parent' support for wp_get_object_terms()): yes, we can do this pretty easily.

More generally, the request reminds me of some previous tickets (eg #18106) that have to do with feature parity between get_terms() and wp_get_object_terms(). The two functions have different use cases, but there's no reason why wp_get_object_terms() shouldn't support many of the arguments supported by get_terms(). I've opened #31105 for consideration of this broader issue.

In the meantime, I think it's fine to duct tape a fix for 'parent'.

#8 @boonebgorges
9 years ago

  • Resolution set to fixed
  • Status changed from new to closed

[31270] missed the ticket.

Note: See TracTickets for help on using tickets.