admin管理员组

文章数量:1122846

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

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'
        )
    )
);
Share Improve this question edited Jul 27, 2015 at 7:29 Pieter Goosen 55.4k23 gold badges115 silver badges209 bronze badges asked Jul 27, 2015 at 7:11 user1903471user1903471 111 silver badge2 bronze badges 3
  • This issue has been handled before, please use the site search, you should get your answer in the returned search results ;-) – Pieter Goosen Commented Jul 27, 2015 at 7:30
  • Thanks Pieter, I have search about this issue but not much help. – user1903471 Commented Jul 27, 2015 at 7:47
  • Really? ;-) Here is a search with 249 results – Pieter Goosen Commented Jul 27, 2015 at 7:50
Add a comment  | 

1 Answer 1

Reset to default 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' );

本文标签: Create custom query for search