robindee
robindee

Reputation: 11

Query Wordpress Posts for multiple meta keys with multiple values

Im having trouble with my WordPress query on a project I do for a customer of us. Basically my problem is that I want to grab posts from the database with specified meta_keys. I want to get my posts (wpcompare) with the meta key '_price' and value between xx and yy. Works fine so far. Now I want to add filters for manufacturer, categories and tags. These filter values are all multiple, so that you can select multiple manufacturers. For example Canon and Nikon. So with the WP_MetaQuery I can filter multiple meta_keys and values, but I cant combine the queries with AND or OR.

Let me give you an example, how my query should work:

"Give me all the posts with post_type "wpcompare" where the meta_value _price is between 100 and 200 bucks, and where the manufacturer (_hersteller) is Canon OR Nikon OR Sony".

My head turns crazy, so please help me.

Thank you very much in advance :-)

Here is my Code:

if(isset($_POST) AND !empty($_POST))
{
    $meta_query = array(
        'relation' => 'AND',
        array(
            'key' => '_price',
            'value' => array($_POST['p_from'], $_POST['p_to']),
            'type' => 'CHAR',
            'compare' => 'BETWEEN'
        ),

    );
}
else
{
    $meta_query = '';
}

$args = array(
    'post_type' => 'wpcompare',
    'post_status' => 'publish',   
    'paged' => $paged,
    'meta_query' => $meta_query,
    'posts_per_page' => ($per_page == false) ? 18 : $per_page,
    'ignore_sticky_posts'=> true
);

$temp = $wp_query;

$wp_query = null;
$wp_query = new WP_Query($args)

Upvotes: 1

Views: 6242

Answers (2)

Vijay Lathiya
Vijay Lathiya

Reputation: 1227

$args = array(
    'post_type' => 'posttypehere',
    'meta_query' => array(
       'relation' => 'OR',
        array(
              'key' => '_price',
              'value' => array($_POST['p_from'], $_POST['p_to']),
              'type' => 'CHAR',
              'compare' => 'BETWEEN'
              ), 
         array(
            'key' => 'somekey',
            'value' => array($_POST['p_from'], $_POST['p_to']),
            'type' => 'CHAR',
            'compare' => 'BETWEEN'
             ),
         array(
            'key' => 'anotherkey',
            'value' => array($_POST['p_from'], $_POST['p_to']),
            'type' => 'CHAR',
            'compare' => 'BETWEEN'
        ),

    )
);

$query = new WP_Query( $args );
if ( $query->have_posts() ) :
    while ($query->have_posts()) : $query->the_post();
             echo $post_id = get_the_ID();
    endwhile;
endif;

Upvotes: 3

Prince Singh
Prince Singh

Reputation: 5183

$meta_query = array(
        'relation' => 'OR',
        array(
            'key' => '_price',
            'value' => array($_POST['p_from'], $_POST['p_to']),
            'type' => 'CHAR',
            'compare' => 'BETWEEN'
        ), 
        array(
            'key' => 'somekey',
            'value' => array($_POST['p_from'], $_POST['p_to']),
            'type' => 'CHAR',
            'compare' => 'BETWEEN'
        ),
         array(
            'key' => 'anotherkey',
            'value' => array($_POST['p_from'], $_POST['p_to']),
            'type' => 'CHAR',
            'compare' => 'BETWEEN'
        ),

    );

Upvotes: 1

Related Questions