• I m trying to implement a multiple meta key filter in my wordpress. That’s simple I get value to a form to filter my post. If I implement this with only “price” query wordked perfectly. If I add “genre” nothing work, query not working.

    For field “genre” I m using checkbox from Advanced Custom Fields with this structure “homme : Homme / femme : Femme”.

    I test different thing like delete “price” and query on “genre” not working too…

    I get value from this

    ` <?php
    if($_GET[‘minprice’] && !empty($_GET[‘minprice’]))
    {
    $minprice = $_GET[‘minprice’];
    } else {
    $minprice = 0;
    }

    if($_GET[‘maxprice’] && !empty($_GET[‘maxprice’]))
    {
    $maxprice = $_GET[‘maxprice’];
    } else {
    $maxprice = 1000;
    }

    if($_GET[‘genre’] && !empty($_GET[‘genre’]))
    {
    $genre = $_GET[‘genre’];
    }
    ?>`

    my query looks like this

    ` $args = array(
    ‘cat’ => $cat,
    ‘post_type’ => ‘post’,
    ‘posts_per_page’ => 28,
    ‘paged’ => $paged,
    ‘meta_query’ => array(
    ‘relation’ => ‘AND’,

    array(
    ‘key’ => ‘prix’,
    ‘type’ => ‘NUMERIC’,
    ‘value’ => array($minprice, $maxprice),
    ‘compare’ => ‘BETWEEN’
    ),

    array(
    ‘key’ => ‘genre’,
    ‘value’ => $genre,
    ‘compare’ => ‘LIKE’
    )
    )
    );`

    My loop with my query

    ` <?php
    // set up or arguments for our custom query
    $paged = ( get_query_var(‘paged’) ) ? get_query_var(‘paged’) : 1;
    $args = array(
    ‘cat’ => $cat,
    ‘post_type’ => ‘post’,
    ‘posts_per_page’ => 28,
    ‘paged’ => $paged,
    ‘meta_query’ => array(
    ‘relation’ => ‘AND’,

    array(
    ‘key’ => ‘prix’,
    ‘type’ => ‘NUMERIC’,
    ‘value’ => array($minprice, $maxprice),
    ‘compare’ => ‘BETWEEN’
    ),

    array(
    ‘key’ => ‘genre’,
    ‘value’ => $genre,
    ‘compare’ => ‘LIKE’
    )
    )
    );

    // create a new instance of WP_Query
    $the_query = new WP_Query($args);
    ?>

    <?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); // run the loop ?>

    <?php
    get_template_part( ‘content-category’, get_post_format() );
    ?>

    <?php endwhile; ?>

    <?php if ($the_query->max_num_pages > 1) { // check if the max number of pages is greater than 1 ?>
    <div class=”clearfix”></div>

    <?php bootstrap_pagination();?>

    <?php } ?>

    <?php else: ?>
    <?php get_template_part( ‘no-results’, ‘archive’ ); ?>
    <?php endif; ?>
    </div>
    <?php wp_reset_query(); ?>`

    I tested this and it’s work !

         $args = array(
                	'cat' => $cat,
                	'post_type' => 'post',
                	'posts_per_page' => 28,
                	'paged' => $paged,
                	'meta_query' => array(
                			array(
                				'key' => 'prix',
                				'type' => 'NUMERIC',
                				'value' => array($minprice, $maxprice),
                				'compare' => 'BETWEEN'
                				), 
                
                			)
                		);

    But that don’t work

    ` $args = array(
    ‘cat’ => $cat,
    ‘post_type’ => ‘post’,
    ‘posts_per_page’ => 28,
    ‘paged’ => $paged,
    ‘meta_query’ => array(
    array(
    ‘key’ => ‘genre’,
    ‘value’ => $genre,
    ‘compare’ => ‘LIKE’
    )
    )
    );`

    Please, can you help me beacause I m loosing my mind….
    Thanks !

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    Without knowing what your database contains and not being able to verify that each variable has a proper value, it’s very difficult to help you out. The best thing you can do is debug your own work.

    Start by hooking the “posts_request” filter and outputting the passed SQL query string. Usually by examining the actual query you can determine where the problem lies. Add this hook just before you create a new WP_Query to avoid extraneous output from other queries like menus and widgets. You can even have your callback remove itself after it outputs the proper query so subsequent queries are not output as well.

Viewing 1 replies (of 1 total)

The topic ‘WP query and multiple key’ is closed to new replies.