1

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)

2 Answers 2

3
$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;
Sign up to request clarification or add additional context in comments.

Comments

1
$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'
        ),

    );

4 Comments

Price comparison works perfectly. thanks a lot, but my problem is about to combine this with the values for multiple manufacturers, categories and tags.
"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". isnt this all you required
for taxonomies such as categories you have use tax_query also look here for tax query codex.wordpress.org/Class_Reference/WP_Query
Sorry, pr1nc3. You were totally right. Problem solved, thanks a lot!

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.