Make WordPress Core

Opened 12 years ago

Closed 12 years ago

#20604 closed defect (bug) (fixed)

Empty tax_query returns all posts

Reported by: ninos-ego's profile Ninos Ego Owned by: ryan's profile ryan
Milestone: 3.5 Priority: normal
Severity: minor Version: 3.3.1
Component: Query Keywords: has-patch
Focuses: Cc:

Description

have got this codesnippet in the args

'tax_query' => array(
	'relation' => 'OR',
	array(
		'taxonomy' => 'taxonomy1',
		'field' => 'id',
		'terms' => 'sometimes false or clear',
		'operator' => 'IN'
	),
	array(
	'taxonomy' => 'taxonomy2',
		'field' => 'id',
		'terms' => 'sometimes false or clear',
		'operator' => 'IN'
	)
),

if in tax 1 and tax 2 there's no term, I get all posts. I think I should get than no posts...

Think, thats a bug, hope you can fix it in the next version

Attachments (1)

tax-query-no-results-on-or.diff (793 bytes) - added by wonderboymusic 12 years ago.

Download all attachments as: .zip

Change History (13)

#1 @scribu
12 years ago

That's consistent with 'meta_query', 'post__in', etc. We should fix it everywhere, if possible.

Last edited 12 years ago by scribu (previous) (diff)

#2 @scribu
12 years ago

  • Summary changed from get_posts tax_query to Empty tax_query returns all posts

#3 @dd32
12 years ago

I'm not sure I'd call this a bug when you're specifically specifying tax_query. I also believe it's been covered in a previous ticket.

Simply ignoring the specific query parts if there isn't content doesn't seem right to me, The above request for example is "I specifically want posts IN tax1 in the list (none) OR IN tax2 in the list (none)", to receive a response of "Here's a list of posts that match the rest of your args, but may not be related to tax1 or tax2 at all" would seem like buggy behaviour to me.

#4 @scribu
12 years ago

Erm... so do you think it's a bug or not?

#5 @dd32
12 years ago

I see the current behaviour as not a bug. I see the proposed behaviour as an unexpected buggy behaviour (to a developer)

Last edited 12 years ago by dd32 (previous) (diff)

#6 follow-up: @scribu
12 years ago

Ok, so you think that "Here's a list of posts that match the rest of your args, but may not be related to tax1 or tax2 at all" is a bug.

But that's not the proposed behavior; it's the current behavior!

#7 in reply to: ↑ 6 @dd32
12 years ago

Replying to scribu:

But that's not the proposed behavior; it's the current behavior!

You're right.. I've misread it.. Sounds like a bug to me if that's the way it's currently acting.

#8 @kovshenin
12 years ago

  • Cc kovshenin@… added

#9 @wonderboymusic
12 years ago

  • Keywords has-patch added; needs-patch removed

Yep - the problem is the OR relation, the script bails the loop early and doesn't tell the where clause that the tax_query is garbage. To test:

$q = new WP_Query( array(
	'fields'	=> 'ids',
	'tax_query' => array(
		'relation' => 'OR',
		array(
			'taxonomy' => 'post_tag',
			'field' => 'id',
			'terms' => false,
			'operator' => 'IN'
		),
		array(
			'taxonomy' => 'category',
			'field' => 'id',
			'terms' => false,
			'operator' => 'IN'
		)
	)	
	
) );

print_r( $q->posts );
exit();

Should return an empty array - does so with my patch, attached.

Last edited 12 years ago by wonderboymusic (previous) (diff)

#10 @scribu
12 years ago

  • Milestone changed from Awaiting Review to 3.5

#12 @ryan
12 years ago

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

In [22109]:

An empty taxonomy query should return no results rather than all posts. Props wonderboymusic. fixes #20604

Note: See TracTickets for help on using tickets.