WordPress.org

Make WordPress Core

Opened 2 years ago

Closed 19 months ago

#20604 closed defect (bug) (fixed)

Empty tax_query returns all posts

Reported by: Ninos Ego Owned by: 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 20 months ago.

Download all attachments as: .zip

Change History (13)

comment:1 scribu2 years ago

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

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

comment:2 scribu2 years ago

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

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

comment:4 scribu2 years ago

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

comment:5 dd322 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 2 years ago by dd32 (previous) (diff)

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

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

comment:8 kovshenin2 years ago

  • Cc kovshenin@… added

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

comment:10 scribu20 months ago

  • Milestone changed from Awaiting Review to 3.5

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