3

I need to do a WP_Query with a LIKE on the post_title and category

This query_post is not working

query_posts(
     array(
        'post_type' => 'add_buying',
        'like' => $keywords,
        'posts_per_page' => 5,
        'taxonomy' => 'add_country',
        'term' => 'drawing'
 ));
1
  • Please state what is not working with your query. Commented Sep 10, 2014 at 9:37

3 Answers 3

8

Change your second parameter to 's' and I think it's going to work:

$args = array(      
    'post_type'   => 'post',
    's'           => $search_term,
    'post_status' => 'publish'    
);
$wp_query = new WP_Query($args);

And good luck

1
  • 1
    adding second parameter with 's' will not only search in titles but also post content . That will not be the most relevant result. Commented Jul 23, 2018 at 9:04
7

Check this url and change the like parameter.

query_posts( array(
'post_type' => 'add_buying',
's' => $keywords,
'posts_per_page' => 5,
'taxonomy' => 'add_country',
'term' => 'drawing' 
));
2
  • 4
    Just as a side note, this also searches with in the content of a post. Commented May 9, 2016 at 20:22
  • I realise that the context of this answer is based on the original question, but as a general note to everyone else: never, ever use query_posts (For reference: stackoverflow.com/a/25589475/988246). It should only ever be used by the core. Use one of the many, many other functions available like WP_Query or get_posts, where the same array of arguments can be used safely.
    – indextwo
    Commented Jun 1, 2016 at 14:32
2

title (string) - use post title (available with Version 4.4).

$args = array(      
    'post_type'   => 'post',
    'title'       => $title,
    'post_status' => 'publish'
); 
$wp_query = new WP_Query($args);

Not the answer you're looking for? Browse other questions tagged or ask your own question.