Home Forums Add-ons Repeater Field acf/fields/post_object/query inside repeater filtered by taxonomy sub_field


acf/fields/post_object/query inside repeater filtered by taxonomy sub_field

  • I have a repeater field “Rankings” with taxonomy field ‘School’ and Post object field ‘Courses’
    Can I use acf/fields/post_object/query to filter the Courses by chosen School? If yes, how should I go about it? Any resources, documentation or related discussions links are appreciated!

  • 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(
                    'taxonomy'  => 'school',
                    'field'     => 'term_id',
                    'terms'     => array($term_id),
        return $args;
  • If you have code that works outside of a repeater you should be able to use the same code for a repeater sub field if you use the field key variant of the hook.

  • Wouldn’t that filter a single post_object field ?
    I basically want to filter each ‘Courses’ post object field by the corresponding ‘School’ taxonomy field
    Attached the image of my repeater structure. In my blog post I have (let’s say) 15 schools

  • My working code filters Courses bases on $post_id of the School taxonomy, however in a case that involves repeaters $post_id would be an ID of my Blog post (as I understand).
    Is it possible to dynamically access the properties of School taxonomy inside of the repeater and if it is – how?

  • I do not have any code or examples or know where to find any.

    You will need to add custom JavaScript to ACF. You will need to alter the AJAX data by getting the value of the taxonomy field and adding it to the data. Then you will need to use that added data in the filter.

    On top of this you will need to figure out how to trigger ACF to refresh that list if the term selected in the taxonomy field is changed. I haven’t got a clue were to start for this. This would probably be the hardest part and I have never seen where anyone has accomplished it.

    If I had to do this I would use 2 dynamically populated select fields and do most of the coding for those fields myself, for instance using the values in the fields to get the correct terms/post. There is a basic example of doing this for posts here that would take some modification to use terms.

  • Getting the value of a sibling field, basic example code.

    var sibling = field.closest('.acf-row').find('[data-key="field_XXXXXXX"]);
  • I see, guess it’s not as simple as it looked.
    I really appreciate your help.
    Will post a solution if I get there

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

You must be logged in to reply to this topic.