Support

Account

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

  • 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.