From dc48f3d795950734e77a851f0b2df61ed4edf754 Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Sat, 22 Aug 2015 16:58:21 +0000 Subject: [PATCH] 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. git-svn-id: https://develop.svn.wordpress.org/trunk@33706 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/class-wp.php | 2 +- src/wp-includes/query.php | 7 +++++++ tests/phpunit/tests/query/results.php | 15 +++++++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/class-wp.php b/src/wp-includes/class-wp.php index 69a45ada48..062240e221 100644 --- a/src/wp-includes/class-wp.php +++ b/src/wp-includes/class-wp.php @@ -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. diff --git a/src/wp-includes/query.php b/src/wp-includes/query.php index 916913d1ce..fff57c25bf 100644 --- a/src/wp-includes/query.php +++ b/src/wp-includes/query.php @@ -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'] ); diff --git a/tests/phpunit/tests/query/results.php b/tests/phpunit/tests/query/results.php index 45fdc1663a..8fed88ada1 100644 --- a/tests/phpunit/tests/query/results.php +++ b/tests/phpunit/tests/query/results.php @@ -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 ); + } }