0

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',
        array(
            'key' => 'postal_code',
            'value' => $keyword,
            'compare' => 'LIKE'
        )
    )
);
3
  • This issue has been handled before, please use the site search, you should get your answer in the returned search results ;-) Commented Jul 27, 2015 at 7:30
  • Thanks Pieter, I have search about this issue but not much help. Commented Jul 27, 2015 at 7:47
  • Really? ;-) Here is a search with 249 results Commented Jul 27, 2015 at 7:50

1 Answer 1

0

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' );

4
  • 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 Commented Jul 27, 2015 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' ); Commented Jul 27, 2015 at 8:00
  • Your query working fine but only for title if $keyword is any postal code result give a null Commented Jul 27, 2015 at 8:46
  • Ohh.. your meta_key should be like 'meta_key' => '_postal_code', just make it sure with your database.. ;) Commented Jul 27, 2015 at 9:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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