0

I'm building custom search. Actually it's just need to check custom field value. The custom field is called 'summary'. However i'm not sure what i'm doing wrong as it is not working. Maybe someone will see what i'm doing wrong?

$thesearch = get_search_query(); // get the string searched

$args = array(
  'post_type'=> 'post',
  'paged'    => $paged,
 'posts_per_page' => 9,
 'meta_query' => array(
   array(
       'key' => 'summary',
       'value' => $thesearch,
       'compare' => 'IN'
      )
     )
  );

  $search = new WP_Query($args);
4
  • 'compare' => 'IN' is for when 'value' is an array. you probably want 'compare' => 'LIKE' or 'compare' => '=' Feb 6, 2015 at 12:37
  • Changed to 'LIKE', but its still not working.
    – MIC
    Feb 6, 2015 at 12:38
  • is the loop outputting nothing or everything? what's the search query you're using to test and what are some example values stored in the meta field? Feb 6, 2015 at 12:52
  • It outputing nothing. Meta field is paragraph text "Just one sentence like summary for the post". I'm searching for 'one' and got nothing.
    – MIC
    Feb 6, 2015 at 12:54

1 Answer 1

0

Found the problem - i was using array, when i just needed to use such arguments:

$args = array(
  'post_type'=> 'post',
  'paged'    => $paged,
  'posts_per_page' => 9,
  'meta_key' => 'summary',
  'meta_value' => $thesearch,
  'meta_compare' => 'LIKE'
  );

Thanks for help guys.

3
  • This should not really make a difference, both syntaxes are correct. It is really strange that the syntax in your question don't work, yet the syntax in your answer do. Feb 6, 2015 at 13:04
  • Yeah, thats a bit srange.
    – MIC
    Feb 6, 2015 at 13:27
  • BTW can i somehow include post title to this query too? Can't find meta key for post title.
    – MIC
    Feb 6, 2015 at 13:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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