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.


git-svn-id: https://develop.svn.wordpress.org/trunk@33706 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Scott Taylor 2015-08-22 16:58:21 +00:00
parent a70e0183e5
commit dc48f3d795
3 changed files with 23 additions and 1 deletions

View File

@ -15,7 +15,7 @@ class WP {
* @access public
* @var array
*/
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');
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');
/**
* Private query variables.

View File

@ -1425,6 +1425,7 @@ class WP_Query {
, 'preview'
, 's'
, 'sentence'
, 'title'
, 'fields'
, 'menu_order'
);
@ -1544,6 +1545,7 @@ class WP_Query {
* true. Note: a string of comma-separated IDs will NOT work.
* @type array $tax_query An associative array of WP_Tax_Query arguments.
* {@see WP_Tax_Query->queries}
* @type string $title Post title.
* @type bool $update_post_meta_cache Whether to update the post meta cache. Default true.
* @type bool $update_post_term_cache Whether to update the post term cache. Default true.
* @type int $w The week number of the year. Default empty. Accepts numbers 0-53.
@ -1577,6 +1579,7 @@ class WP_Query {
$qv['author'] = preg_replace( '|[^0-9,-]|', '', $qv['author'] ); // comma separated list of positive or negative integers
$qv['pagename'] = trim( $qv['pagename'] );
$qv['name'] = trim( $qv['name'] );
$qv['title'] = trim( $qv['title'] );
if ( '' !== $qv['hour'] ) $qv['hour'] = absint($qv['hour']);
if ( '' !== $qv['minute'] ) $qv['minute'] = absint($qv['minute']);
if ( '' !== $qv['second'] ) $qv['second'] = absint($qv['second']);
@ -2605,6 +2608,10 @@ class WP_Query {
unset($ptype_obj);
}
if ( '' !== $q['title'] ) {
$where .= $wpdb->prepare( " AND $wpdb->posts.post_title = %s", stripslashes( $q['title'] ) );
}
// Parameters related to 'post_name'.
if ( '' != $q['name'] ) {
$q['name'] = sanitize_title_for_query( $q['name'] );

View File

@ -703,4 +703,19 @@ class Tests_Query_Results extends WP_UnitTestCase {
$wp_rewrite->flush_rules();
}
function test_title() {
$title = 'Tacos are Cool';
$post_id = $this->factory->post->create( array(
'post_title' => $title,
'post_type' => 'post',
'post_status' => 'publish'
) );
$result1 = $this->q->query( array( 'title' => $title, 'fields' => 'ids' ) );
$this->assertCount( 1, $result1 );
$this->assertContains( $post_id, $result1 );
$result2 = $this->q->query( array( 'title' => 'Tacos', 'fields' => 'ids' ) );
$this->assertCount( 0, $result2 );
}
}