0

Thanks in advance for the help. I'm trying to write a query_posts statement for Wordpress in which:

$args = 'meta_query' => array (
    array (
        array ('key' => 'key-type-1',
               'value' => 'something'
              ),
        array ('key' => 'key-type-2',
               'value' => 'something'
              )
    )
    OR
    array (
        array ('key' => 'key-type-3',
               'value' => 'something'
              ),
        array ('key' => 'key-type-4',
               'value' => 'something'
              )
    )
);
query_posts( $args );

So as you can see, there is a hole in my understanding here :) I am trying to write a scenario in where either can be true: EITHER a value for key-type-1 AND key-type-2 exists, OR a value for key-type-3 AND key-type-4 exists.

I've tried the obvious:

$args = 'meta_query' => array (
    array (
        array ('key' => 'key-type-1',
               'value' => 'something'
              ),
        array ('key' => 'key-type-2',
               'value' => 'something'
              )
    ),
    array (
        array ('key' => 'key-type-3',
               'value' => 'something'
              ),
        array ('key' => 'key-type-4',
               'value' => 'something'
              )
    )
);
query_posts( $args );

But that just produces: A value for key-type-1 AND key-type-2 exists AND a value for key-type-3 AND key-type-4 exists... Which is no good.

Maybe a 'compare' value in each of the sub arrays? Or maybe I have to resort to using multiple query_posts and combining the multiple outputs on display? If anyone has any insights, it would really help me out.

Thanks!

1 Answer 1

0

Firstly, never use query_posts unless your intention is to modify the default Wordpress loop. It modifies all kinds of Globals, it's messy, and it's slow. Please consider changing all of your query_posts calls to WP Query.

There's quite a bit of documentation on the query_posts vs WP_Query debate, and pretty much all come to the conclusion the WP_Query is the way to go.

As for what you want to do, there are a few ways you can go about doing this. A standard SQL call comes to mind, but quite honestly, constructing the SQL might be more trouble than it's worth. Instead, it would probably just be easier to run two separate queries to include posts from both meta queries.

This is largely untested, and I can't guarantee it will work straight out of the gate, but this should get you started:

<?php
$meta_a = array(
    'meta_query' => array(
        array ('key' => 'key-type-1',
               'value' => 'something'
        ),
        array ('key' => 'key-type-2',
               'value' => 'something'
        )
    )
);
$meta_b = array(
    'meta_query' => array(
        array ('key' => 'key-type-3',
               'value' => 'something'
        ),
        array ('key' => 'key-type-4',
               'value' => 'something'
        )
    )
);
$multi_query = array();
$multi_query[] = new WP_Query($meta_a);
$multi_query[] = new WP_Query($meta_b);
foreach($multi_query as $q)
{
    if($q->have_posts()) : while($q->have_posts()) : $q->the_post();
        //YOUR POSTS HERE
    endwhile;endif;
    wp_reset_postdata();
}
?>

UPDATE:

If you absolutely have to use query_posts(), this is how you can implement it:

<?php
$meta = array(
    array(
        array ('key' => 'key-type-1',
               'value' => 'something'
              ),
        array ('key' => 'key-type-2',
               'value' => 'something'
              )
    ),
    array (
        array ('key' => 'key-type-3',
               'value' => 'something'
              ),
        array ('key' => 'key-type-4',
               'value' => 'something'
              )
    )
);
foreach($meta as $args)
{
    query_posts(array('meta_query'=>$args));
    if(have_posts()) : while(have_posts()) : the_post();
        //YOUR POSTS HERE
    endwhile;endif;
    wp_reset_query();
}
?>
Sign up to request clarification or add additional context in comments.

3 Comments

I'll test and report back. I'm basing my site around MapPress Pro in which the author tends to steer folks towards query_posts because it, by default, creates its mashup maps based on the globals, which is not affected by WP_Query. That said, there seems to be a more specific way to integrate the maps plugin to use WP_Query and if I can get that working, I would happily move over to the more accepted way to interact with my db. If I can't get that going, however, are you aware of a way to do what we're talking about here with query_posts? Or is the answer just that it can't be done with it?
I've added an update for you, just in case query_posts is unavoidable. Hope this helps, and good luck.
First off, thank you very much for your input. The update you added using query_posts didn't produce any results unfortunately. Syntax looks great, so I'll keep playing for sure. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.