Support

Account

Home Forums Front-end Issues Query with 2 parameters and 2 meta_key

Solving

Query with 2 parameters and 2 meta_key

  • Hello,

    Thanks for this amazing plugin !

    I m trying to implement a query on my wordpress. I want display post_type “enseignement” with two paramaters like “cycle1”, “cycle2″… and 2 meta_key “cycle”
    and “lieu” like this code above.

    <?php if($_GET['cycle'] && !empty($_GET['cycle']))
    {
    $cycle = $_GET['cycle'];
    } else {
    }
    if($_GET['lieu'] && !empty($_GET['lieu']))
    {
    $lieu = $_GET['lieu'];
    } else {
    }
    ?>
    
    <?php
                    $args = array(
                    'post_type' => 'enseignement',
                    'posts_per_page' => 10,
                    'meta_query' => array(
                             'relation' => 'AND',
                            array(
                                'key' => 'cycle', // name of custom field
                                'value' => $cycle, // matches exactly "red"
                                'compare' => 'LIKE',
                                                            ),
                    array(
                         'key'     => 'lieu',
                         'value'   => $lieu,
                         'compare' => 'LIKE',
    
             ),
        ),
    
                    );
                $loop = new WP_Query( $args );
                while ( $loop->have_posts() ) : $loop->the_post(); ?>
                <?php get_template_part( 'content', 'enseignement', get_post_format() );?>
                <?php endwhile; ?>

    I have url like this this /?cycle=cycle1&lieu=paris and it’s work.

    But if I want multiple “cycle” or multiple “lieu” like this /?cycle=cycle1,cycle2&lieu=paris,marseille I doesn’t work.

    If I m trying to implement
    $cycle = explode(',', $_GET['cycle']);

    Like this

    <?php
    
    $cycle = explode(',', $_GET['cycle']);
    
    				$args = array(
    				'post_type' => 'enseignement',
    				'posts_per_page' => 10,
    				'meta_query' => array(
    						 'relation' => 'AND',
    				        array(
    				            'key' => 'cycle', // name of custom field
    				            'value' => $cycle, // matches exactly "red"
    				            'compare' => 'IN',
    														),
    				array(
    					 'key'     => 'lieu',
    					 'value'   => $lieu,
    					 'compare' => 'LIKE',
    
    		 ),
    	),
    

    and this code doesn’t work too, nothing is show on my archives.

    Please can you help me ?

    Thanks

    By the way I watched your video on vimeo but all my test was unsuccessfull.

  • What type of field are you searching? Are these ACF select fields or something else?
    `

  • Yes thoses fields are ACF select fields

  • You need to use nested meta queries https://make.wordpress.org/core/2014/10/20/update-on-query-improvements-in-4-1/

    and your going to need to dynamically generate the meta query. As an example the cycle portion of it might look something like this

    
    $meta_query = array('relation' => 'AND');
    if ($cycle) {
      $sub_query = array('relation' => 'OR');
      $cycles = explode(',', $cycle);
      foreach ($cycles as $cycle) {
        $sub_query[] = array(
          'key' => 'cycle',
          'value' => '"'.$cycle.'"'
          'compare' => 'LIKE'
        );
      } // end foreach cycle
      $meta_query[] = $sub_query;
    } // end if cycle
    

    Please note that the above has not been tested and is meant only as an example.

    A word of caution, too many LIKE and nested LIKE queries could time out the request.

Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Query with 2 parameters and 2 meta_key’ is closed to new replies.