Support

Account

Home Forums ACF PRO Dynamically Set Taxonomy Filter For Post Object Field

Solved

Dynamically Set Taxonomy Filter For Post Object Field

  • I have a Post Object custom field named “free_course_option” for Taxonomy terms. I’m trying to dynamically set the “Filter by Taxonomy” parameter so that when I am on the edit screen for a Taxonomy term, the taxonomy filter is set to the current term. I’ve tried using acf/load_field and acf/fields/post_object/query but have been unsuccessful with either. So far, using acf/load_field is getting me closer to accomplishing what I want. Here is the code I’ve inserted in my functions.php file:

    
    function acf_load_course_choices( $field ) {
        
      $screen = get_current_screen();
    
      if( $screen->id === "edit-programs" ) {
        if (isset($_GET['tag_ID'])) {
          $tag_ID = $_GET['tag_ID'];
          $this_program = get_term_by('id', $tag_ID, 'programs');
          $this_slug = $this_program->slug;
          // Filter based on which taxonomy (Program) you're currently editing
          $field['taxonomy'] = array();
          $field['taxonomy'][0] = 'programs:' . $this_slug;
        }
      }
    
      return $field;
        
    }
    add_filter('acf/load_field/name=free_course_option', 'acf_load_course_choices');
    
  • I had a similar requirement to yours and found this when searching for a solution.

    I have managed to get this working using the acf/fields/post_object/query filter.

    The way it brings in the information is a bit harder to debug as it’s all AJAX, but using WP_DEBUG_LOG and error_log to see what data was parsing through helped me come to the solution:

    function cxx_event_balance_individual_option( $args, $field, $post_id ) {
    	
    	// $post_id comes in here as term_# so we need to remove 'term_' to get the term ID
    	$prefix = 'term_';
    	
    	// Also if you are creating a new taxonomy, post_id = 'term_0' so then there's no point in adding a filter
    	if ( 'term_0' != $post_id && substr( $post_id, 0, strlen( $prefix )) == $prefix ) {
    		// Set $term_id to the ID part of $post_id
    		$term_id = substr( $post_id, strlen( $prefix ) );
    		
    		// And adjust the query to filter by specific taxonomy term
    		$args['tax_query'] = array(
    			array(
    				'taxonomy'  => 'product_event',
    				'field'     => 'term_id',
    				'terms'     => $term_id,
    			),
    		);
    	}
    	return $args;
    }

    Hope this helps you out.

  • @jakeodb thank you very much for your solution. I applied it to my project and it is working as intended. I guess I was on the right track when I began exploring the acf/fields/post_object/query filter, but I didn’t keep pressing on. Thanks again!

  • Hello, this is kind of old, but I am looking for the same thing and the solution above is not working for me. I don’t know if something changed with later versions, but is there a working solution for the latest ACF version?

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

The topic ‘Dynamically Set Taxonomy Filter For Post Object Field’ is closed to new replies.