I have create search box using ajax.If i enter any value in search input box that this value search in post title or post meta data and get all data of this value in custom post type.also query check only post title and specific post meta data. Not check in Post content or any other field.

I have tried this query

$args = array(
    's' => $keyword,
    'numberposts' => -1,
    'post_type' => 'store',
    'meta_query' => array(
        'relation' => 'OR',
            'key' => 'postal_code',
            'value' => $keyword,
            'compare' => 'LIKE'
  • This issue has been handled before, please use the site search, you should get your answer in the returned search results ;-) – Pieter Goosen Jul 27 '15 at 7:30
  • Thanks Pieter, I have search about this issue but not much help. – user1903471 Jul 27 '15 at 7:47
  • Really? ;-) Here is a search with 249 results – Pieter Goosen Jul 27 '15 at 7:50

Have you check your result-set, what result you are getting in $query obj. another thing, there is no need to use 'relation' => 'OR', in meta query array, have a try with this as well. And instead of 'numberposts' use 'posts_per_page' this will help. :) or you can remove the meta_query and make your $arg something like

$args = array( 's' => $keyword, 'post_type'=> 'store', 'posts_per_page' => -1, 'meta_key' => 'postal_code', 'meta_value' => $keyword, 'meta_compare' => 'LIKE' );

  • Thanks Hari For reply, I have removed 'relation' => 'OR' also i have used 'posts_per_page' but not help. About Result set, no post found – user1903471 Jul 27 '15 at 7:46
  • are you sure, your variable $keyword have some value, or you can use this way, remove the meta_query array... $args = array( 's' => $keyword, 'post_type'=> 'store', 'posts_per_page' => -1, 'meta_key' => 'postal_code', 'meta_value' => $keyword, 'meta_compare' => 'LIKE' ); – Hari Om Gupta Jul 27 '15 at 8:00
  • Your query working fine but only for title if $keyword is any postal code result give a null – user1903471 Jul 27 '15 at 8:46
  • Ohh.. your meta_key should be like 'meta_key' => '_postal_code', just make it sure with your database.. ;) – Hari Om Gupta Jul 27 '15 at 9:17

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.