Support

Account

Home Forums General Issues Content Filtering Using Fields – Issues

Solved

Content Filtering Using Fields – Issues

  • Hello,
    I’m having trouble setting up front-end content filters based on checkbox fields for custom post type.
    As outlined here:
    https://www.advancedcustomfields.com/resources/creating-wp-archive-custom-field-filter/

    Working from the tutorial and the updated code I’ve run into two issues.

        1. When the archive page initially loads with no arguments in the URL, the checkboxes are pre-populated with the field criteria from the latest posts listed.
        2. When I check the boxes, the page is reloaded but No posts are listed even though there are posts with the proper criteria. The URL is updated with the proper arguments and the checkboxes stay checked.

    Wondering what I’m doing wrong here.

    Thanks for any help.
    Using:
    ACF Pro 5.9.3
    WP 5.5.3
    PHP 7.4

  • I think I see my issue.
    Using Checkboxes and not Radio buttons. So my compare was wrong.
    LIKE

    As outlined here.
    https://support.advancedcustomfields.com/forums/topic/filtering-archives-using-checkbox-custom-fields/

    However, now I have a new problem. When filtering by multiple fields, it only takes the last argument in the URL, and isn’t showing multiple posts when multiple checkboxes are selected for one field.

    Example.
    Size: Small, Large
    Color: Black, White

    /catalog/?size=small,large&color=black,white

    I need to show all that have either Small or Large, AND Black or White.
    I’m only getting results that are White (all sizes, no Black).

    Any help or direction is appreciated. Thanks for your time.

  • 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;
    	
    	
    	// bail early if not main query
    	// - allows custom code / plugins to continue working
    	if( !$query->is_main_query() ) 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
    	foreach( $value as $val )
        {
        	$meta_query = [
    	 [
    	     'key'		=> $name,
                'value'		=> '"' . $val . '"',
                'compare'	=> 'LIKE',
            ],
    	];
    }
            
    	} 
    	
    	
    	// update meta query
    	$query->set('meta_query', $meta_query);
    
    }
    
  • When multiple values are passed for multiple fields then you’ll need to use multiple nested meta queries.

    Looking at this query string ?size=small,large&color=black,white

    I’m going to assume that you want

    
    (size == small OR size == large) AND (color == black OR color == white)
    

    You’ll need to figure out out to put this into your loop, but the meta query you would need is something like this

    
    $meta_query = array(
      'relation' => 'AND',
       array(
         'relation' => 'OR',
          array(
            'key' => 'size',
            'value' => '"small"',
            'compare' => 'LIKE'
          ),
          array(
            'key' => 'size',
            'value' => '"large"',
            'compare' => 'LIKE'
          ),
       array(
         'relation' => 'OR',
          array(
            'key' => 'color',
            'value' => '"black"',
            'compare' => 'LIKE'
          ),
          array(
            'key' => 'color',
            'value' => '"white"',
            'compare' => 'LIKE'
          ),
       ),
    );
    

    A word of caution/warning, as the number filters grow and the number of selections made in each filter grows these like queries will take longer and longer to process until they finally time out and likely time out until it prevents the page from loading.

  • Thanks John,
    I see where you’re going. And good caution on having too many filters/Queries.
    Using “foreach”, maybe something like this?

    // append meta query
            $meta_query = array(
              'relation' => 'AND',
    	foreach( $value as $val )
        {
        	array(
             'relation' => 'OR',
    	  array(
    	    'key'	=> $name,
                'value'	=> '"' . $val . '"',
                'compare'	=> 'LIKE',
            ),
    	),
         });

    I went a different route and decided to utilize custom taxonomies, in combination with the Taxonomy field. But still utilizing the Javascript Elliot outlined in the tutorial. Seems to be less intensive query and built into WordPress already.

    Thanks again for your response.

  • Good call on using taxonomies, they are built to do this type of searching in a more efficient way

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

You must be logged in to reply to this topic.