I am using a custom query to generate my search results. The code I am using works perfectly without the meta_query variables in my arguments array.

I am trying to compare multiple custom fields with 'LIKE'.

References to $per_page & $sort_by

$paged = (get_query_var('page')) ? get_query_var('page') : 1;

$per_page = ($_REQUEST['per_page']) ? $_REQUEST['per_page'] : 15;
$sort_by = ($_REQUEST['sort_by']) ? $_REQUEST['sort_by'] : 'best';

Array for query arguments:

$default = array(
    's' =>  $search,        
    'paged'     =>  $paged,
    'post_type' =>  'page',
    'suppress_filters' => false,
    'posts_per_page'    =>  $per_page,
    'meta_key'  =>  '_wp_page_template',
    'meta_value'    =>  'lois_product.php',
    'meta_query'    =>  array(
        'relation'  =>  'OR',
            'key'       =>  'lois_prodpage_colour',
            'value'     =>  $search,
            'compare'   =>  'LIKE'
            'key'       =>  'lois_prodpage_brand',
            'value'     =>  $search,
            'compare'   =>  'LIKE'

Then I just call the arguments as you would expect..

$products = new WP_Query($default);

Loop through etc...

So as you can see... pretty simple stuff to be honest. I just cannot work out why it is returning 0 results as I know I have data to match specific terms or a combination..

This is for a custom WP ecommerce website which is integrated with an external EPoS System (I know)

Any help/pointers would be great!

  • The query looks correct and does not generate any errors when I try it. I imagine the search is just not matching. Edit the output of var_dump($products->request); into your question. Maybe that will help but I think you are going to have to analyze the query and work out what doesn't match. – s_ha_dum May 13 '13 at 14:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.