Make WordPress Core

Changeset 25239


Ignore:
Timestamp:
09/04/2013 06:50:04 PM (12 years ago)
Author:
wonderboymusic
Message:

Kill the query in the following edge case: post_type => 'any' but exclude_from_search => false returns no valid post types. Adds unit tests.

Props mitchoyoshitaka.
Fixes #19198.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/query.php

    r25238 r25239  
    24562456        if ( 'any' == $post_type ) {
    24572457            $in_search_post_types = get_post_types( array('exclude_from_search' => false) );
    2458             if ( ! empty( $in_search_post_types ) )
     2458            if ( empty( $in_search_post_types ) )
     2459                $where .= ' AND 1=0 ';
     2460            else   
    24592461                $where .= " AND $wpdb->posts.post_type IN ('" . join("', '", $in_search_post_types ) . "')";
    24602462        } elseif ( !empty( $post_type ) && is_array( $post_type ) ) {
  • trunk/tests/phpunit/tests/query/results.php

    r25002 r25239  
    370370        ), wp_list_pluck( $posts, 'post_title' ) );
    371371    }
     372
     373    function test_exlude_from_search_empty() {
     374        global $wp_post_types;
     375        foreach ( array_keys( $wp_post_types ) as $slug )
     376            $wp_post_types[$slug]->exclude_from_search = true;
     377
     378        $posts = $this->q->query( array( 'post_type' => 'any' ) );
     379
     380        $this->assertEmpty( $posts );
     381        $this->assertRegExp( '#AND 1=0#', $this->q->request );
     382
     383        foreach ( array_keys( $wp_post_types ) as $slug )
     384            $wp_post_types[$slug]->exclude_from_search = false;
     385
     386        $posts2 = $this->q->query( array( 'post_type' => 'any' ) );
     387
     388        $this->assertNotEmpty( $posts2 );
     389        $this->assertNotRegExp( '#AND 1=0#', $this->q->request );
     390    }
    372391}
Note: See TracChangeset for help on using the changeset viewer.