WordPress.org

Make WordPress Core

Changeset 33706


Ignore:
Timestamp:
08/22/2015 04:58:21 PM (3 years ago)
Author:
wonderboymusic
Message:

Query:

Add a query var, title, that allows you to query posts by post_title. To accomplish this now, you have to do something like:

$tacos = get_posts( [
  'post_type' => 'taco',
  's' => $name,
  'exact' => true,
  'sentence' => true,
  'post_status' => 'publish',
  'fields' => 'ids',
  'posts_per_page' => 1
] );

Adds unit tests.

Fixes #33074.

Location:
trunk
Files:
3 edited

Legend:

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

    r33658 r33706  
    1616     * @var array
    1717     */
    18     public $public_query_vars = array('m', 'p', 'posts', 'w', 'cat', 'withcomments', 'withoutcomments', 's', 'search', 'exact', 'sentence', 'calendar', 'page', 'paged', 'more', 'tb', 'pb', 'author', 'order', 'orderby', 'year', 'monthnum', 'day', 'hour', 'minute', 'second', 'name', 'category_name', 'tag', 'feed', 'author_name', 'static', 'pagename', 'page_id', 'error', 'comments_popup', 'attachment', 'attachment_id', 'subpost', 'subpost_id', 'preview', 'robots', 'taxonomy', 'term', 'cpage', 'post_type');
     18    public $public_query_vars = array('m', 'p', 'posts', 'w', 'cat', 'withcomments', 'withoutcomments', 's', 'search', 'exact', 'sentence', 'calendar', 'page', 'paged', 'more', 'tb', 'pb', 'author', 'order', 'orderby', 'year', 'monthnum', 'day', 'hour', 'minute', 'second', 'name', 'category_name', 'tag', 'feed', 'author_name', 'static', 'pagename', 'page_id', 'error', 'comments_popup', 'attachment', 'attachment_id', 'subpost', 'subpost_id', 'preview', 'robots', 'taxonomy', 'term', 'cpage', 'post_type', 'title');
    1919
    2020    /**
  • trunk/src/wp-includes/query.php

    r33661 r33706  
    14261426            , 's'
    14271427            , 'sentence'
     1428            , 'title'
    14281429            , 'fields'
    14291430            , 'menu_order'
     
    15451546     *     @type array        $tax_query               An associative array of WP_Tax_Query arguments.
    15461547     *                                                 {@see WP_Tax_Query->queries}
     1548     *     @type string       $title                   Post title.
    15471549     *     @type bool         $update_post_meta_cache  Whether to update the post meta cache. Default true.
    15481550     *     @type bool         $update_post_term_cache  Whether to update the post term cache. Default true.
     
    15781580        $qv['pagename'] = trim( $qv['pagename'] );
    15791581        $qv['name'] = trim( $qv['name'] );
     1582        $qv['title'] = trim( $qv['title'] );
    15801583        if ( '' !== $qv['hour'] ) $qv['hour'] = absint($qv['hour']);
    15811584        if ( '' !== $qv['minute'] ) $qv['minute'] = absint($qv['minute']);
     
    26062609        }
    26072610
     2611        if ( '' !== $q['title'] ) {
     2612            $where .= $wpdb->prepare( " AND $wpdb->posts.post_title = %s", stripslashes( $q['title'] ) );
     2613        }
     2614
    26082615        // Parameters related to 'post_name'.
    26092616        if ( '' != $q['name'] ) {
  • trunk/tests/phpunit/tests/query/results.php

    r31324 r33706  
    704704    }
    705705
     706    function test_title() {
     707        $title = 'Tacos are Cool';
     708        $post_id = $this->factory->post->create( array(
     709            'post_title' => $title,
     710            'post_type' => 'post',
     711            'post_status' => 'publish'
     712        ) );
     713
     714        $result1 = $this->q->query( array( 'title' => $title, 'fields' => 'ids' ) );
     715        $this->assertCount( 1, $result1 );
     716        $this->assertContains( $post_id, $result1 );
     717
     718        $result2 = $this->q->query( array( 'title' => 'Tacos', 'fields' => 'ids' ) );
     719        $this->assertCount( 0, $result2 );
     720    }
    706721}
Note: See TracChangeset for help on using the changeset viewer.