Home › Forums › Front-end Issues › Post Object Field – Set selected value programatically › Reply To: Post Object Field – Set selected value programatically
I believe you can do it by using the acf/prepare_field hook. Here’s an example to do it:
function my_acf_prepare_field4( $field ) {
if( isset($_GET['post']) ){
$post_id = $_GET['post'];
$field['value'] = $post_id;
}
return $field;
}
// name
add_filter('acf/prepare_field/name=custom_field_name', 'my_acf_prepare_field4');
ACF uses select2 for the post object. To do it, please take a look at this thread: http://stackoverflow.com/questions/19639951/how-do-i-change-selected-value-of-select2-dropdown-with-jqgrid. Also, please take a look at this page to learn more how to use ACF with Javascript: https://www.advancedcustomfields.com/resources/adding-custom-javascript-fields/.
I hope this helps 🙂
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!
We’re working hard on Advanced Custom Fields PRO 6.0, and Beta 1 is now available 🚀
— Advanced Custom Fields (@wp_acf) August 12, 2022
Featuring improved performance for Repeater fields with large datasets, and a new generation of ACF Blocks.
Let’s take a look 🧵https://t.co/Befre3kFAo
© 2022 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.