Home Forums ACF PRO Filter a Post Object with AJAX


Filter a Post Object with AJAX

  • Sorry for the repeat thread. I thought of a different approach to my question, and a more straightforward way to explain it.


    Alright… I decided it’s probably more appropriate to break this issue down into smaller chunks… then maybe… as a community, we can address one at a time, and provide a workable solution 🙂

    The setup:

    • I have 3 CPTs: Sites, Products, Orders
    • Sites has a Repeater Field called: Site_Products, with a Post Object Field (that returns Post ID) called: Site_Product
    • Orders has 2 Post Object Fields (that return Post ID) called: Site and Product

    The goal:

    1. Have Product only have Options that are specific to the Selected Site

    Steps that I *believe* are needed:

    1. Enqueue a JavaScript file
    2. The JavaScript should wait for the “On Change” event for the Site field, and pass that value to a PHP function?
    3. The PHP function should gather up the Products specific to that Site, and create a JSON file to pass back to the JavaScript?
    4. The JavaScript then adds Options to the Product Post Object Selector?

    Seems simple enough in theory 🙂

    Now, so far… I have successfully enqueued an empty JavaScript to satisfy Step 1 above.

    Here’s the code that I added to a custom plugin:

    function acf_admin_enqueue( $hook ) { 
      $type = get_post_type(); // Check current post type
      $types = array( 'orders' ); // Allowed post types
      if( !in_array( $type, $types ) )
        return; // Only applies to post types in array
      wp_enqueue_script( 'populate-products', plugin_dir_url( __FILE__ ) . 'populateproducts.js' );
      wp_localize_script( 'populate-products', 'pp_vars', array(
        'pp_nonce' => wp_create_nonce( 'pp_nonce' ), // Create nonce which we later will use to verify AJAX request
    add_action( 'admin_enqueue_scripts', 'acf_admin_enqueue' );

    Can someone pretty please, with sugar on top, tell me how to handle step 2? 🙂

  • I don’t know if this will help you, but I published several examples of dynamically populating ACF fields of various types using AJAX. You can look them over here

  • Hi John,

    Thanks for the great examples. I can really use many of those in my work! Great resource. I believe I recognize your name as someone who makes my life easier when generating dynamic content for Contact Form 7 too… so thanks for that!!

    I combed through your ACF AJAX examples… and I think that the *only* thing you haven’t covered is for my situation described above lol Everything else seems to be there 🙂

    Can you show me which one you think is the closest based on what I described?

    Thanks again… I appreciate your contributions to ACF, CF7, and WordPress overall.


  • You mentioned, maybe in your other post about a country/state type of situation, one of my examples deals with that. I don’t think that any of my examples deals specifically with what you want, but then I’m not sure I completely understand what you are looking to do.

    Reading your other post a bit, I’m not sure that you can dynamically populate a post object field the way that I’ve done with other types of fields, basically because these fields are select2 fields that are already populated using ajax. To make changes to what’s loaded in these fields you would need to figure out how to add the value of your “site” field to the ajax query that ACF is already using and then use that value in an acf/fields/post_object/query filter It would probably be easier to stick to vanilla select fields.

    You also mentioned something about populating a repeater, and there is an example of that.

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

The topic ‘Filter a Post Object with AJAX’ is closed to new replies.