WordPress.org

Make WordPress Core

Opened 9 years ago

Closed 9 years ago

#15350 closed defect (bug) (fixed)

WP_Query Assumes Taxonomy Query Operator

Reported by: filosofo Owned by: filosofo
Milestone: 3.1 Priority: normal
Severity: normal Version: 3.1
Component: Query Keywords:
Focuses: Cc:

Description

The following query works in WP 3.0 and seems to be syntactically valid:

query_posts( array(
   'taxonomy' => 'post_tag',
   'term' => 'my term',
) );

However, trunk throws a notice because no operator is defined.

Patch checks for the operator to be set before attempting to evaluate it.

Attachments (1)

dont-assume-tax-operator.15350.diff (486 bytes) - added by filosofo 9 years ago.

Download all attachments as: .zip

Change History (9)

#1 @scribu
9 years ago

  • Keywords has-patch removed

That will prevent the appropriate query flag from being set.

#2 @scribu
9 years ago

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

(In [16258]) Set 'operator' field. Fixes #15350. See #12891

#3 @filosofo
9 years ago

  • Resolution fixed deleted
  • Status changed from closed to reopened

OK, but that changes existing behavior.

In 3.0:

query_posts( array(
   'taxonomy' => 'post_tag',
   'term' => 'my tag',
) );
var_dump( is_tax() ); // false

With r16258:

query_posts( array(
   'taxonomy' => 'post_tag',
   'term' => 'my tag',
) );
var_dump( is_tax() ); // true

Is this change of behavior intentional?

#4 @scribu
9 years ago

Can't reproduce.

Even if I could, [16258] would not be the cause.

#5 @filosofo
9 years ago

What can't you reproduce? The 3.0 behavior or trunk behavior?

r163258 is the proximate cause, because prior to that the above query has no operator set (that's what this ticket is about).

From wp-includes/query.php:

1576                 foreach ( $q['tax_query'] as $query ) {
1577                         if ( 'IN' == $query['operator'] ) {
1578                                 switch ( $query['taxonomy'] ) {
1579                                         case 'category':
1580                                                 $this->is_category = true;
1581                                                 break;
1582                                         case 'post_tag':
1583                                                 $this->is_tag = true;
1584                                                 break;
1585                                         default:
1586                                                 $this->is_tax = true;
1587                                 }
1588                         }
1589                 }    

According to the above, if the operator isn't IN, then is_category and is_tag will not be true.

#7 @scribu
9 years ago

An operator should always be present.

Otherwise, a correct check would be:

if ( !isset( $query['operator'] ) || 'IN' == $query['operator'] ) {

#8 @scribu
9 years ago

  • Resolution set to fixed
  • Status changed from reopened to closed
Note: See TracTickets for help on using tickets.