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:
The goal:
Steps that I *believe* are needed:
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 https://github.com/Hube2/acf-dynamic-ajax-select-example
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.
Keith
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 https://www.advancedcustomfields.com/resources/acf-fields-post_object-query/. 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.
You must be logged in to reply to this topic.
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!
CPT registration is coming to ACF! We demoed the new feature during the most recent session of ACF Chat Fridays. Check out the summary for the details. https://t.co/k2KQ3WWBAz
— Advanced Custom Fields (@wp_acf) March 7, 2023
© 2023 Advanced Custom Fields.
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.