meta_query, how to search using both relation OR & AND?

前端 未结 2 1395
孤城傲影
孤城傲影 2021-02-01 03:28

Resolved: See answer below.


I have a custom post type called BOOKS. It has several custom fields, named: TITLE, AUTHOR, GE

2条回答
  •  抹茶落季
    2021-02-01 04:08

    After a bit trial and error I find a solution to this. This is logical I mean meta_query does not supports the array for field "key", but giving the array in "key", I'm getting the perfect solution. I may be sound crazy but it's working like charm.

    $args => array(
        'relation' => 'AND',
        array(
            'key' => array('title','author','genre',),
            'value' => $searchvalue,
            'compare' => '='
        ),
        array(
            'key' => 'rating',
            'value' => $ratingvalue,
            'compare' => '=',
            'type' => 'NUMERIC'
    
        )
    )
    

    You only get a warning for "trim()" as we are passing an array instead of a string. Suppress that warning or Please add something if you find a better solution.

提交回复
热议问题