Make WordPress Core

Opened 10 years ago

Closed 9 years ago

#28439 closed enhancement (wontfix)

Add 'post_date' as a possible orderby argument to get_terms()

Reported by: jchristopher's profile jchristopher Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.9.1
Component: Taxonomy Keywords: has-patch needs-unit-tests
Focuses: Cc:

Description

There have been a couple of times where I needed to retrieve a list of taxonomy terms ordered by the post_date of their applicable posts. This patch considers post_date as a potential orderby argument value in get_terms().

Attachments (2)

28439.patch (1.0 KB) - added by jchristopher 10 years ago.
Add orderby argument of post_date to get_terms()
28439.2.patch (1.1 KB) - added by jchristopher 10 years ago.
To prevent duplicate terms, SELECT DISTINCT when orderby = 'post_date'

Download all attachments as: .zip

Change History (7)

@jchristopher
10 years ago

Add orderby argument of post_date to get_terms()

#1 follow-up: @lpointet
10 years ago

How does this query behaves when a same term is applied to multiple posts?
Because joining on posts may return multiple times the same terms for instance.

@jchristopher
10 years ago

To prevent duplicate terms, SELECT DISTINCT when orderby = 'post_date'

#2 in reply to: ↑ 1 @jchristopher
10 years ago

  • Keywords has-patch added

Replying to lpointet:

How does this query behaves when a same term is applied to multiple posts?
Because joining on posts may return multiple times the same terms for instance.

You're absolutely right! 28439.2.patch ensures SELECT DISTINCT to avoid duplicate terms.

#3 @jchristopher
10 years ago

  • Keywords needs-unit-tests added

Here's an EXPLAIN from my test site which admittedly only has 4 Categories and 3 Posts:

EXPLAIN SELECT t.*, tt.* 
  FROM wpsvn_terms AS t 
INNER JOIN wpsvn_term_taxonomy AS tt 
  ON t.term_id = tt.term_id 
JOIN wpsvn_term_relationships AS tr 
  ON tr.term_taxonomy_id = tt.term_taxonomy_id 
JOIN wpsvn_posts AS p 
  ON p.ID = tr.object_id 
WHERE tt.taxonomy IN ('category') 
ORDER BY p.post_date DESC 
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE tt ALL PRIMARY,term_id_taxonomy,taxonomy NULL NULL NULL 4 Using where; Using temporary; Using filesort
1 SIMPLE tr ref PRIMARY,term_taxonomy_id term_taxonomy_id 8 wpsvn.tt.term_taxonomy_id 1 Using index
1 SIMPLE t ALL PRIMARY NULL NULL NULL 4 Using where; Using join buffer
1 SIMPLE p eq_ref PRIMARY PRIMARY 8 wpsvn.tr.object_id 1

#4 @boonebgorges
9 years ago

See https://core.trac.wordpress.org/ticket/18106#comment:7. I'd make the same argument here: get_terms() seems like the wrong place for it, and it's hard to see how the abstraction would work in wp_get_object_terms().

#5 @boonebgorges
9 years ago

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

Gonna close this bad boy for the reasons outlined in #18106. You can finagle it by doing get_posts( array( 'tax_query' => ... ) ) to get the post order, then looping through results, grabbing term IDs (which will have been loaded into the cache, so no real overhead added), and then passing to get_terms( 'my_taxonomy', array( 'orderby' => 'include', 'include' => $term_ids ) ).

Note: See TracTickets for help on using tickets.