WordPress.org

Make WordPress Core

Opened 14 months ago

Closed 9 months ago

#20604 closed defect (bug) (fixed)

Empty tax_query returns all posts

Reported by: Ninos Ego Owned by: ryan
Priority: normal Milestone: 3.5
Component: Query Version: 3.3.1
Severity: minor Keywords: has-patch
Cc: me@…, kovshenin@…

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 10 months ago.

Download all attachments as: .zip

Change History (13)

comment:1 scribu14 months ago

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

Last edited 14 months ago by scribu (previous) (diff)

comment:2 scribu14 months ago

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

comment:3 dd3214 months 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.

comment:4 scribu14 months ago

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

comment:5 dd3214 months 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 14 months ago by dd32 (previous) (diff)

comment:6 follow-up: scribu14 months 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!

comment:7 in reply to: ↑ 6 dd3214 months 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.

comment:8 kovshenin14 months ago

  • Cc kovshenin@… added

comment:9 wonderboymusic10 months 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 10 months ago by wonderboymusic (previous) (diff)

comment:10 scribu10 months ago

  • Milestone changed from Awaiting Review to 3.5

comment:12 ryan9 months 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.