0

I'm facing a rather strange issue when attempting to pass in four different meta_query values to my WP_Query.

Find below an example of my WP_Query search. Please bear in mind, if I remove and test each of these meta_query on there own, they work fine. It's just when they are being passed in as four do I not get any results. Even if I leave the values blank I still receive no posts in my query!

$v_args = array(
    'post_type'     =>  'loads_available',
    's'             =>  "Post Name", 
    'meta_query'    =>  array(
      array(
            'key'     => 'type_of_load',
            'value'   => "Frozen",
            'compare' => '=',
          ),
        array(
            'key'     => 'number_of_pallets',
            'value'   => 2,
            'compare' => '>=',
            'type' => 'NUMERIC'

          ),
        array(
            'key'     => 'type_of_trailer_required',
            'value'   => "Trailor",
            'compare' => '=',
          ),
          // ^ problem
        array(
            'key' => 'euro_pallets',
            'value' => "Yes",
            'compare' => '='

            )
        )
    );

    $loadsAvailableQuery = new WP_Query( $v_args );

Now I would just like to make it clear, these meta_query values work fine on a individual basis, it's just when I start trying to use them in conjunction with each other for example.

Search query would be:

Post Name: "Post Name" Type of Load: "Frozen" Number of Pallets: 5 Type of Trailor Required: "Trailor" Euro Pallets: "Yes"

This would not give me any results, even though I am sure there is posts meeting the criteria. It's feels like I could be missing something in the Query, I read about use relation but that defaults to "OR" - which I believe would be the correct value here.

Are there any common misconceptions or obvious flaws in my code that I'm not quite seeing here?

Many thanks for the responses.

  • Does it work if you use any 2 (or 3) together? – bonger Jul 18 '15 at 9:39
  • Yes, it seems that I can use 2 at the same time anymore and it just doesn't load results (yet there are results available...) even if the value is empty – Nicholas Rowe Jul 20 '15 at 21:32
  • So you mean it fails if you use any 2 together? Do any posts satisfy both (or all) conditions? Did you try setting 'relation' => 'OR'? – bonger Jul 20 '15 at 22:48
  • Ideally, I want the relation to be AND because I want the query to match all of the search values. – Nicholas Rowe Jul 23 '15 at 8:24
  • Yes but you're in a debug situation here so trying OR could lead you to see what data is matching and whether it meets your expectations ... also it might be worth doing a var_dump($loadsAvailableQuery->request); after the new and running the query directly against the database (via mysql shell or phpAdmin or whatever)... – bonger Jul 23 '15 at 8:41
0

You may want to try adding the 'RELATION' => parameter into your meta query array so that you can encompass all of your meta values. Something like this:

    $v_args = array(
    'post_type'     =>  'loads_available',
    's'             =>  "Post Name", 
    'meta_query'    =>  array(
      'relation' => 'AND',
      array(
            'key'     => 'type_of_load',
            'value'   => "Frozen",
            'compare' => '=',
          ),
        array(
            'key'     => 'number_of_pallets',
            'value'   => 2,
            'compare' => '>=',
            'type' => 'NUMERIC'

          ),
        array(
            'key'     => 'type_of_trailer_required',
            'value'   => "Trailor",
            'compare' => '=',
          ),
          // ^ problem
        array(
            'key' => 'euro_pallets',
            'value' => "Yes",
            'compare' => '='

            )
        )
    );

    $loadsAvailableQuery = new WP_Query( $v_args );

Depending on your use case, you may need to set up a variable based on the user query to toggle the relation parameter in your query.

Note the relation parameter can also be set to 'OR'.

Checkout the meta_query section inside of the WP_Query codex

Your Answer

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

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