Support

Account

Home Forums Front-end Issues pre_get_posts Custom post filter by custom fields

Unread

pre_get_posts Custom post filter by custom fields

    • Webic

    • January 10, 2019 at 6:31 am

    Hi,
    I have created a custom post type Car and some custom field.
    Now I tried to make filterable cars in archive page, but when I checked one of value of field all cars hide and checkbox filter disappear… I don’t understand why…
    This is code in functions.php

    // array of filters (field key => field name)
    $GLOBALS['my_query_filters'] = array( 
    	'carburante' => 'carburante'
    );
    
    add_action('pre_get_posts', 'filter_car_posts', 10, 1);
    
    function filter_car_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( 'car' ) ) 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',  
            );
    	}
        
    	// update meta query
    	$query->set( 'meta_query', $meta_query);
        $query->set( 'posts_per_page', '20');
        
    }

    And this is code for filtering option

    <?php foreach( $GLOBALS['my_query_filters'] as $key => $name ): 
    
    					// get the field's settings without attempting to load a value
    					$field = get_field_object($key, false, false);
    
    					// set value if available
    					if( isset($_GET[ $name ]) ) {
    						$field['value'] = explode(',', $_GET[ $name ]);
    					}
    					// create filter
    					?>
    					<div class="filter" data-filter="<?php echo $name; ?>">
    						<?php create_field( $field ); ?>
    					</div>
    
    				<?php endforeach; ?>
    				</div>
    
    				<script type="text/javascript">
    				(function($) {
    					// change
    					$('#archive-filters').on('change', 'input[type="checkbox"]', function(){
    						// vars
    						var url = '<?php echo home_url('car'); ?>';
    							args = {};
    						// loop over filters
    						$('#archive-filters .filter').each(function(){
    							// vars
    							var filter = $(this).data('filter'),
    								vals = [];
    							// find checked inputs
    							$(this).find('input:checked').each(function(){
    								vals.push( $(this).val() );
    							});
    							// append to args
    							args[ filter ] = vals.join(',');
    						});
    						// update url
    						url += '?';
    						// loop over args
    						$.each(args, function( name, value ){
    							url += name + '=' + value + '&';
    						});
    						// remove last &
    						url = url.slice(0, -1);
    						// reload page
    						window.location.replace( url );
    					});
    				})(jQuery);
    				</script>
Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.