Home › Forums › Add-ons › Repeater Field › acf/fields/post_object/query inside repeater filtered by taxonomy sub_field › Reply To: acf/fields/post_object/query inside repeater filtered by taxonomy sub_field
This is what worked for filtering inside the Taxonomy Term Editor ( no repeater )
add_filter('acf/fields/post_object/query/name=courses_list', 'filter_by_category');
function filter_by_category( $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' => 'school',
'field' => 'term_id',
'terms' => array($term_id),
),
);
}
return $args;
}
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.