Opened 12 months ago

Last modified 12 days ago

#20767 new defect (bug)

tax_query clobbers tag and category

Reported by: wonderboymusic Owned by:
Priority: normal Milestone: Future Release
Component: Query Version: 3.1
Severity: major Keywords: has-patch
Cc:

Description

The main query should return a tag or category as its queried object if tax_query is applied to a tag or category archive page. It currently returns the first term derived from the WP_Query->tax_query->queries array which does not include the tag or category as a term in the request. WP_Query->tax_query->queries is a portion of the request, but not the request. Because WP_Query sets is_tag and is_category flags, they have to be captured before dealing with is_tax

The result of this bug is that theme functions return the wrong data because the wrong term is returned with get_queried_object.

Real world example: at eMusic, we pass regions into pre_get_posts to regionalize the content that is returned. On a tag page, if you are in the US, we pass the regions "ALL" and "US" to make sure you are looking at appropriate content. Here's what the title for the archives looks like - (spoiler: It's "ALL" when it should be "Daily Download"):

http://www.emusic.com/17dots/topics/daily-download/

I am attaching a patch.

Attachments (2)

tag-cat-queried-obj.diff (1.1 KB) - added by wonderboymusic 12 months ago.
Patch for WP_Query->get_queried_object
20767.diff (1.0 KB) - added by wonderboymusic 4 months ago.

Download all attachments as: .zip

Change History (9)

Patch for WP_Query->get_queried_object

comment:1 follow-up: ↓ 3   scribu12 months ago

I'm not sure using $this->get( 'cat' ) is much better: you can have ?cat=1,2,-3.

  • Version changed from 3.4 to 3.1

comment:3 in reply to: ↑ 1   wonderboymusic11 months ago

Replying to scribu:

I'm not sure using $this->get( 'cat' ) is much better: you can have ?cat=1,2,-3.

It works, if you have a URL like so: www.emusic.com/17dots/topics/daily-download/?cat=1,2,3,4

  • is_tag is true
  • is_category is true
  • cat returns 1 (the first category id)
  • tag_id returns 8376, which is correct

The public query is correct:

array(2) {
    'cat' =>
    string(7) "1,2,3,4"
    'tag' =>
    string(14) "daily-download"
}

is_tag takes precedence of is_category and should

Version 0, edited 11 months ago by wonderboymusic (next)

can I get a 3.5 amen from the congregation? tax_query has been breaking theme functions for a while now

I really don't see the issue. If you handle category or post_tag as a custom taxonomy you wouldn't have the issue right?
I rather go for something like that then adding code for only category or post_tag

  • Milestone changed from Awaiting Review to 3.6

The fact that we are already doing this:

if ( $this->is_category || $this->is_tag || $this->is_tax ) {
    ....
}

...means we need to check them before arbitrarily grabbing the first term in the stack

comment:7   ryan12 days ago

  • Milestone changed from 3.6 to Future Release
Note: See TracTickets for help on using tickets.