Make WordPress Core

Opened 10 years ago

Closed 10 years ago

#30941 closed feature request (invalid)

Get taxonomy terms by post ID and order/orderby

Reported by: miyarakira's profile miyarakira Owned by:
Milestone: Priority: normal
Severity: minor Version: 4.1
Component: Taxonomy Keywords: close
Focuses: Cc:

Description

I have a situation where I need to get taxonomy terms, optionally by associated post ID, and sorted by order/orderby.

The issue:

  • get_terms accepts order/orderby (and other parameters) but not post ID, as far as I could see
  • get_the_terms accepts post ID but not order/orderby.

So when I need the terms by post ID, I can't have them sorted. Here's another person who wanted to sort by term slug, and the recommendation was to sort them manually after get_the_terms.

I read the Codex on both functions and looked into their respective code: get_terms in taxonomy.php, and get_the_terms in category-template.php. Is it possible to achieve what I want currently? If not, it would be helpful to have either:

1) order/orderby in get_the_terms
2) post_id in get_terms

Change History (5)

#1 @miyarakira
10 years ago

  • Severity changed from normal to minor

It seems the first option would be easier to achieve: get_the_terms could accept an optional third parameter, to be passed as argument to wp_get_object_terms (which does take order/orderby).

Version 1, edited 10 years ago by miyarakira (previous) (next) (diff)

#2 @boonebgorges
10 years ago

Hi miyarakira - Why not just use wp_get_object_terms() directly? (The problem with adding support in get_terms() is that the we'll have to have separate caches for each value of the $args passed to wp_get_object_terms()).

#3 @miyarakira
10 years ago

Thank you for the suggestion, that solved my issue.

  • Get taxonomy terms without post ID: get_terms
  • Get taxonomy terms with post ID and order/orderby: wp_get_object_terms
  • Get taxonomy terms with post ID without order/orderby: get_the_terms to take advantage of the cache

Well, it looks like I should have posted this in the support forum instead of raising a ticket - at least I learned something new. During the process I set up a development version of WordPress via SVN, so I'm able to submit patches to core now - cool! :)

#4 @miyarakira
10 years ago

  • Keywords close added

#5 @netweb
10 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from new to closed

@miyarakira Creating a ticket here is a small price to pay now that you've learned something new and you've now got a WordPress SVN checkout, we'll all look forward to seeing your first patch and props :)

Note: See TracTickets for help on using tickets.