Support

Account

Home Forums ACF PRO Archive with ajax custom field filter : Error 500 Reply To: Archive with ajax custom field filter : Error 500

  • No problem. I updated my code :

    $GLOBALS['my_query_filters'] = array(
    	'field_5cb6ef1f75209'  => 'alcool',
    );
    
    // 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(
    			array(
    				'key'     => $name,
    				'value'       => $value,
    				'compare'  => 'IN',
    			)
    		);
    
    	}
    
    	// update meta query
    	$query->set('meta_query', $meta_query);
    	return;
    
    }
    

    But it doesn’t work. I don’t have any result when I select a checkbox filter. I think checkbox field is stored as serialized array, so I can not use the IN operator and array (if I select multiple checkbox filters).
    I think I have to create a loop with an OR relation in my meta_query.