Just very generally – I’m trying to do the following at the post level in admin:
On change of relationship picker:
1. get meta data of that post
2. Populate the input value of another field with meta data from step 1
The idea is that we would be providing suggested copy for those user to use.
Is it possible to access the object of that post, like we do in the templates? Or do I need to grab the ID (I see it in the data attribute) and ajax in the post?
Yes, I can give you some direction, but I can’t give you details on your exact case. Honestly, there’s not a lot of information available on doing what you want to do. I have been working on a project that does similar things.
You can see an example of what I’m doing here: https://github.com/Hube2/blunt-parametric-search/blob/master/admin/js/admin.js
This script extends the acf.ajax script. In it I have created change events that fire when certain fields have been changes. Two of the functions do AJAX requests to to get information that’s used to populate other fields. The functions are _para_tax_change and _para_meta_key_change.
Getting values from every type of field is slightly different, I have not worked with a relationship field, which is why a said I can’t give specifics. What I do is just start adding
alert(something); in the script to see what I can figure out and go from there.
You can see the PHP side of the AJAX requests in this file https://github.com/Hube2/blunt-parametric-search/blob/master/admin/php/acf-load.php. You can see where the filters are added in the __construct function and the related functions that are called.
This file contains an example of how I enqueue the script https://github.com/Hube2/blunt-parametric-search/blob/master/admin/php/admin-post-types.php
I realize that’s a long way from an perfect answer but I hope it helps.
I really appreciate the direction, thanks for your help!
Thanks again, I was able to use your on change code to understand how the relationship behaved. I also used http://v2.wp-api.org/ and https://wordpress.org/plugins/acf-to-rest-api/ to pass the ID: (‘.values input’).val() in order to pull in the acf meta from the selected post.
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!
🤔 Curious about the ACF user experience? So are we! Help guide the evolution of ACF by taking part in our first ever Annual Survey and guarantee you’re represented in the results. https://t.co/0cgr9ZFOJ5— Advanced Custom Fields (@wp_acf) May 8, 2023
© 2023 Advanced Custom Fields.