Is it possible to query posts by a specific word on title?
Example:
I have 3 posts inside my blog and only two have the word car on the title.
- She wants a ride on my truck
- My car is super fast
- Her car is yellow
If this is possible, query should return only posts 2 and 3.
Any help is greatly appreciated.
Is it possible to query posts by a specific word on title?
Example:
I have 3 posts inside my blog and only two have the word car on the title.
- She wants a ride on my truck
- My car is super fast
- Her car is yellow
If this is possible, query should return only posts 2 and 3.
Any help is greatly appreciated.
Share Improve this question edited Oct 12, 2015 at 22:40 bpy asked Oct 12, 2015 at 22:33 bpybpy 2995 silver badges20 bronze badges 2 |1 Answer
Reset to default 2The answer to this question is here.
The full credit of the answer to my question goes for the author of the answer birgire.
All I had to do was slightly change is plugin "Support for post name like in WP_Query" turning it into a function like this:
add_filter( 'posts_where', 'title_like_posts_where', 10, 2 );
function title_like_posts_where( $where, $q) {
if( $name__like = $q->get( '_name__like' ) )
{
global $wpdb;
$where .= $wpdb->prepare(
" AND {$wpdb->posts}.post_name LIKE %s ",
str_replace(
array( '**', '*' ),
array( '*', '%' ),
mb_strtolower( $wpdb->esc_like( $name__like ) )
)
);
}
return $where;
}
A special thanks to Pieter Goosen for pointing my out to the solution.
WHERE post_content LIKE '%$query%'
withWHERE post_title LIKE '%$query%'
– Ismail Commented Oct 12, 2015 at 22:44