Support

Account

Home Forums General Issues Archive Filter with Multiple Values Select Field

Solving

Archive Filter with Multiple Values Select Field

  • I’m following the guide on this page – https://www.advancedcustomfields.com/resources/creating-wp-archive-custom-field-filter/ and so far I’ve got everything working for Select fields with only one value. However, this doesn’t seem to work when a Select field is able to have multiple values.

    This is my current code:

    
    <?php
    
    // array of filters (field key => field name)
    $GLOBALS['my_query_filters'] = array(
      'classes'       => 'classes',
      'level'	  => 'level',
      'time'          => 'time',
    );
    
    // 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;
    
    	// 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
        	$meta_query[] = array(
                'key'	=> $name,
                'value'	=> $value,
                'compare'	=> 'IN',
            );
    
    	}
    
    	// update meta query
    	$query->set('meta_query', $meta_query);
    
    }
    

    It works perfectly fine whenever I add “?level=xx” or “?time=xx” to the URL, and even if I add both “?level=xx&time=xx”. But adding “?class=xx” doesn’t work at all.

    I’m guessing it’s breaking because the “Class” Select field allows multiple values, and I read that it saves those kinds of fields differently?

    I tried searching all over for a solution, but I couldn’t figure it out. I did read a bunch of stuff about querying an array, but I wasn’t able to figure out how to add that to this code.

  • Basically, on this page https://www.advancedcustomfields.com/resources/query-posts-custom-fields/ if you look in the section 3. Multiple custom field values (array based values) it tells you how to query based on these values. I understand it and my advice is this, ignore it and don’t do it. Even if you get it to work if you add too many “LIKE” queries for the same meta key you’ll just end up timing out your site because of the limits of WP_Query.

    Instead make your life easier. You know that you can use an “IN” query with normal WP post meta fields, you already have an example of this. So instead of trying to search the ACF field, which is difficult, convert the ACF field into something that is easy to search.

    
    add_filter('acf/save_post', 'convert_classes_to_standard_wp', 20);
    function convert_classes_to_standard_wp($post_id) {
      // use a different field name for your converted value
      $meta_key = 'converted_classes';
      // clear any previously stored values
      delete_post_meta($post_id, $meta_key);
      // get new acf value
      $values = get_field('classes', $post_id);
      if (is_array($values) && count($values) {
        foreach ($values as $value) {
          add_post_meta($post_id, $meta_key, $value, true);
        } // end foreach
      } // end if
    }
    

    Now you can use converted_classes instead of classes in your meta query with the 'compare' => 'IN'.

  • Where exactly do I put that in my current code? I tried adding it but it gave me a 500 error.

  • should go in your functions.php file

    there is a missing ) in the code which is the cause of the 500

    
    add_filter('acf/save_post', 'convert_classes_to_standard_wp', 20);
    function convert_classes_to_standard_wp($post_id) {
      // use a different field name for your converted value
      $meta_key = 'converted_classes';
      // clear any previously stored values
      delete_post_meta($post_id, $meta_key);
      // get new acf value
      $values = get_field('classes', $post_id);
      if (is_array($values) && count($values)) {
        foreach ($values as $value) {
          add_post_meta($post_id, $meta_key, $value, true);
        } // end foreach
      } // end if
    }
    
  • Okay, it’s working almost the way it should. It’s only working if I have the Select field set to return “Values” and not “Labels”.

    I can, however, get it to work with “Labels” if I change a line to
    $values = get_field('classes', $post_id, false);
    But then it only shows one result at a time. Like if multiple posts have a class value of “8”, it’s only displaying one.

  • Okay nevermind, it works with “Label” if I change the url to “?converted_classes=[label]” instead of using the value in the url.

    However, it’s only working for the first selection. For example, if a post has both the classes “Class_A” and “Class_B”, then only “?converted_classes=class_a” works.

    EDIT: I just noticed, looking down at the default WP custom fields section on the page after I save/update, that it’s only adding a meta value for the first one. So if I select “Class_A” and “Class_B”, it’s not even adding the meta value for “Class_B”.

  • Sorry for the delayed response to this.

    There was an error in the code I posted above

    add_post_meta($post_id, $meta_key, $value, true);

    should be

    add_post_meta($post_id, $meta_key, $value, false);

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

The topic ‘Archive Filter with Multiple Values Select Field’ is closed to new replies.