Support

Account

Home Forums General Issues Query in URL for ACF chexbox field in CPT Reply To: Query in URL for ACF chexbox field in CPT

  • Hi @daisuke

    Here you go, this code has been tried and tested and confirmed working:

    add_action('pre_get_posts', 'filter_posts_by_acf'); 
    function filter_posts_by_acf( $query ) {
    	
    	if(is_admin()){
    		return;
    	}	
    
    	$meta_query = $query->get('meta_query');
    	
    	if( isset($_GET['coup_de_coeur']) ){
    		$meta_query		= [];
    		$meta_query[]	= array(
    			'key'		=> 'coup_de_coeur',
    			'value'		=> $_GET['coup_de_coeur'],
    			'compare'	=> 'LIKE',
    		);
    	}
    	
    	$query->set('meta_query', $meta_query);
    	
    	return $query;
    
    }

    Adding your code initially, I got an error but its because I already had a function with the same name, so changed it.
    I then added some validation and a quick tidy.