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 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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.