Support

Account

Forum Replies Created

  • Hi John,

    I think so. I also have a custom post type (“actions”) so it should work for you.

    Cheers

  • Here’s the working code, in case it can help others.
    The main problem of the previous one was that I set the queries multiple times, while here I append to it using $meta_query[] =

    <?php
    // array of filters (field key => field name)
            $GLOBALS['my_query_filters'] = array( 
                'action'	=> 'action', 
                'ville'	    => 'ville', 
                'date'	    => 'date'
            );
    
    add_action('pre_get_posts', 'my_pre_get_posts', 10, 1);
    
    function my_pre_get_posts( $query ) {
    	
    	// bail early if is in admin
    	if( is_admin() ) return;
    	
    	// bail early if not main query
    	// - allows custom code / plugins to continue working
    	if( !$query->is_main_query() ) return;
    
        if( ! is_post_type_archive( 'actions' ) ) return;
        
        
         // CITY/TYPE/DATE filters
        // loop over filters
    	foreach( $GLOBALS['my_query_filters'] as $key => $name ) {
    		
    		// continue if not found in url
    		if( empty($_GET[ $name ]) ) { continue; }
    		
    		// get the value for this filter
    		// eg: http://www.website.com/events?city=melbourne,sydney
    		$value = explode(',', $_GET[ $name ]);
                
            // append meta query
        	$meta_query[] = array(
                'key'		=> $name,
                'value'		=> $value,
                'compare'	=> 'IN',  
            );
    	}
        
            // FUTURE/PAST/ALL filters
        if( isset($_GET['range']) ) {
            
        $range = $_GET['range'];
    
          if($range != "toutes") {
              
              // Select what kind of compare you need
              if($range == "past") {
                $comparer = '<';
              } else if($range == "future") {
                $comparer = '>=';
              }
            
            // If you need to filter by date add this to meta_query array
            $meta_query[] = array(
                'key'     => 'date',
                'value'   => date("Ymd"),
                'compare' => $comparer,
                );
              }
           }
        
       
        
    	// update meta query
    	$query->set('meta_query', $meta_query);
        
        $query->set( 'meta_key', 'date' );
        $query->set( 'orderby', array( 'date' => 'DESC' ) );
        $query->set('posts_per_page', '20');
        
    }
    
    ?>
Viewing 3 posts - 1 through 3 (of 3 total)