Muaaz Khalid
Muaaz Khalid

Reputation: 2249

WordPress: search by Term, Post meta OR Post title together

we are writing a custom search function which searches for the posts by given Term OR Post Meta OR post_title

We have multiple terms and Post Meta values and We're looking for the solution to apply OR operation on few meta & term values.

bellow is the query that we have so far but ofcourse, is not working as we required

$search_keyword = $_GET['search_keyword'];

$args = array(
    'post_type' => 'product',
    'post_title' => $search_keyword,
    'tax_query' => array(
        array(
            'taxonomy' => 'property-city',
            'terms' => $search_keyword,
            'field' => 'slug',
            'operator'  => 'LIKE'
        ),
        array(
            'taxonomy' => 'property-type',
            'terms' => array('sale', 'rent'),
            'field' => 'slug',
            'operator'  => 'IN'
        )
    ),
    'meta_query' => array(
        array(
            'key' => 'min_beds',
            'value' => '2',
            'compare' => '>=',
        ),
        array(
            'key' => 'min_baths',
            'value' => '1',
            'compare' => '>=',
        ),
        array(
            'key' => 'address',
            'value' => $search_keyword,
            'compare' => 'LIKE',
        )
    )
);

$the_query = new WP_Query($args);

$search_keyword contains the string value that needs to search. And only those posts should be selected that have a meta key matched with given $search_keyword OR matched with term OR matched with Post Title

also, keeping the other conditions as it is.

NOTE: relation b/w Taxonomy property-type and Post Meta min_beds/min_baths should be AND

Upvotes: 3

Views: 3371

Answers (1)

user1049961
user1049961

Reputation: 2736

I'm pretty sure this is not possible. You can either write custom SQL, or just do two or three queries and merge the results - that would probably be far easiest.

Upvotes: 2

Related Questions