Home Forums General Issues Filter post object based on selected taxonomy


Filter post object based on selected taxonomy

  • Hello,
    I have a pretty simple app. there are two selects on an options page. the first select is for an admin to select a taxonomy (some category). then I need the next select to be able to populate with posts that have that category.

    I was thinking this would be somewhat easy but I’m having a hard time since I can’t use acf/fields/post_object/query (docs) because I need the value of the category field which I don’t think I can get with PHP.

    I’ve been messing around with the ajax filters and found select2_ajax_data (docs) but I’m unsure how to change the query.

    Appreciate any help, Thanks!

  • Using the select2_ajax_data hook in JS you need to get the value of the other field you want to filter by and and add that to the data.

    acf.add_filter('select2_ajax_data', function( data, args, $input, field, instance ){
        // do something to data
        // return
        return data;

    I don’t really know all the details for using this. I would assume that the current field that the ajax query is being done for will be in field so you would need to check this to see if it is the field you want to change it for.

    I would add the basic script (above) and then I would start using console.log() to start outputting stuff to figure it out.

    Then use acf.Field() to get the value of the field.

    Once you have the data added then you would use an acf/fields/post_object/query filter to alter the query based on the data value you’ve added, This will be in $_GET.

  • Awesome Thank you John. the $_GET was the missing piece. I was actually unable to access the variable with $_GET but $_POST and $_REQUEST both seemed to work. Thanks again.

  • I was probably wrong about $_GET vs $_POST. I can never remember which is used without looking or testing.

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

You must be logged in to reply to this topic.