WordPress.org

Make WordPress Core

Opened 4 years ago

Closed 3 months ago

#15675 closed enhancement (fixed)

Add a "parent" criteria to wp_get_object_terms()

Reported by: 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 4 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)

@mikeschinkel4 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 @mikeschinkel4 years ago

  • Cc mikeschinkel@… added

comment:2 @scribu4 years ago

  • Keywords has-patch added

comment:3 @nacin4 years ago

  • Milestone changed from Awaiting Review to Future Release

comment:4 @wonderboymusic21 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 @landwire14 months 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?

comment:6 @Mte9011 months ago

Any news about this patch?

comment:7 @boonebgorges3 months 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'.

comment:8 @boonebgorges3 months ago

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

[31270] missed the ticket.

Note: See TracTickets for help on using tickets.