Support

Account

Home Forums General Issues GET (url) parameters for data arrays? Reply To: GET (url) parameters for data arrays?

  • Hi John, thanks for this, I didn’t know they were treated similar to a relationship. Here is my current functions.php code based on Elliot’s tutorial:

    // array of filters (field key => field name)
    $GLOBALS['my_query_filters'] = array( 
    	'example_field'   => 'example1', 
    	'example_field_two'  =>   'example2',
    	//'music-taste' => 'music-taste' (this is what I'm trying to make work)
    );
    
    // action
    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;
    	}
    	
    	// get meta query
    	$meta_query = $query->get('meta_query');
    
    	// 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',
            );  
    	} 
    	// update meta query
    	$query->set('meta_query', $meta_query);
    }

    I’m unsure of how to incorporate your suggested code, being as my working queries are ‘compare’ => ‘IN’, and my checkboxes need to be ‘compare’ => ‘LIKE’.

    Many thanks!