post_title
isn’t a valid argument to WP_Query
. If you dump the SQL you will see that it is ignored:
string(189) “SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts
WHERE 1=1 AND wp_posts.post_type=”investigator” AND
(wp_posts.post_status=”publish”) ORDER BY wp_posts.post_title ASC
LIMIT 0, 6″
You can use name
(or pagename
) instead, but you cannot push raw SQL through like that. It won’t work:
string(158) “SELECT wp_posts.* FROM wp_posts WHERE 1=1 AND
wp_posts.post_name=”like-%d00d” AND wp_posts.post_type=”investigator” ORDER BY wp_posts.post_title ASC “
If you use the s
argument– like this 's' => $_POST['s']
–, you get something like you are looking for:
string(310) “SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts
WHERE 1=1 AND (((wp_posts.post_title LIKE ‘%d00d%’) OR
(wp_posts.post_content LIKE ‘%d00d%’))) AND (wp_posts.post_password =
”) AND wp_posts.post_type=”investigator” AND (wp_posts.post_status=”publish”) ORDER BY wp_posts.post_title ASC LIMIT 0, 6″
But you are searching more fields than just the title. To restrict that to the title only, you can use a filter from another of my answers:
function only_title_search_wpse_119422( $search ) {
remove_filter( 'posts_search', 'only_title_search_wpse_119422' );
global $wpdb;
$pattern = "/\({$wpdb->posts}.post_title[^)]+\)/";
preg_match_all( $pattern, $search, $matches );
if ( ! empty( $matches[0] ) ) {
$search = sprintf(
" AND (%s)",
implode( " AND ", $matches[0] )
);
}
return $search;
}
add_filter( 'posts_search', 'only_title_search_wpse_119422' );