Support

Account

Home Forums General Issues Filter two fields

Helping

Filter two fields

  • Hello
    I am struggling with using filters to search within posts using two fields:
    • Onderwerp
    • Type instelling
    ACV

    I am using functions.php to filter:

    <?php
    add_action('pre_get_posts', 'my_pre_get_posts');
     
     function my_pre_get_posts( $query )
    {
    	// validate
    	if( is_admin() )
    	{
    		return;
    	}
     	// get original meta query
    	$meta_query = $query->get('meta_query');
     
            // allow the url to alter the query
            // eg: http://www.website.com/events?location=melbourne
            // eg: http://www.website.com/events?location=sydney
            if( !empty($_GET['onderwerp']) )
            {
            	$onderwerp = explode(',', $_GET['onderwerp']);
     
            	//Add our meta query to the original meta queries
    	    	$meta_query[] = array(
                    'key'		=> 'onderwerp',
                    'value'		=> $onderwerp,
                    'compare'	=> 'IN',
                );
            }
     	// update the meta query args
    	$query->set('meta_query', $meta_query);
     
    	// always return
    	return;
     
    }
    ?>
    

    However, how can i filter two fields: onderwerp and type_instelling?
    What is the url like in this case?
    Also how can I display the results? I am now using:

    <div id="primary" class="content-area">
    		<div id="content" class="site-content" role="main">
    		<?php if ( have_posts() ) : ?>
    		
    		<div id="search-cursussen">
    	<?php 
     
    	$field = get_field_object('onderwerp');
    	$values = explode(',', $_GET['onderwerp']);
     
     
    	?>
    	<ul>
    		<?php foreach( $field['choices'] as $choice_value => $choice_label ): ?>
    			<li>
    				<input type="checkbox" value="<?php echo $choice_value; ?>" <?php if( in_array($choice_value, $values) ): ?>checked="checked"<?php endif; ?> /> <?php echo $choice_label; ?></li>
    			</li>
    		<?php endforeach; ?>
    	</ul>
    </div>
    <script type="text/javascript">
    (function($) {
     
    	$('#search-cursussen').on('change', 'input[type="checkbox"]', function(){
     
    		// vars
    		var $ul = $(this).closest('ul'),
    			vals = [];
     
    		$ul.find('input:checked').each(function(){
     
    			vals.push( $(this).val() );
     
    		});
     
    		vals = vals.join(",");
     
    		window.location.replace('<?php echo home_url(''); ?>?onderwerp=' + vals);
    		
     
    		console.log( vals );
     
    	});
     
    })(jQuery);	
    </script>
    

    Thanks for the help, i am struggling with this for some time now..

  • Hi @almer1984

    It is possible to query more than one custom field by first allowing for multiple $_GET variables in your pre_get_posts filter. To do this, simply copy and paste your onderwerp code and change the field_name.

    You will need to change quite a bit of your JS to allow for multiple values to be added to the URL. I would advise that you write a function that returns the URL based on all the selected checkbox values.

    You can then attach another ‘click’ event to the second checkbox inputs and use this function to redirect the URL.

    I would just like to point out that this PHP and JS logic is quite advanced, and I won’t be able to help much on this free ACF support forum. If the task seems too complicated, perhaps try breaking it up into steps and research these independently.

    Hope that helps.

    Thanks
    E

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

The topic ‘Filter two fields’ is closed to new replies.