Hi,
I’m using ACF Pro, specifically acf_form(), in conjunction with CPTOnomies (https://wordpress.org/plugins/cpt-onomies/).
I’m using CPTOnomies as a taxonomy (my_post_type, which acts like both a post and taxonomy) for a custom post type. For the custom post type, CPTOnomy values are stored as the ‘meta_key’ ‘_custom_post_type_onomies_relationship.’
I need to populate an ACF field from my CPTOnomy Loop, and have this working just fine through the code below.
//Populate the Select field, 'My Post Type' (field_1234) with dynamic values
add_filter('acf/load_field/key=field_1234', function($my_cpt_field) {
// Cptonomy - city Loop
$cptonomy_arr = get_posts( array(
'numberposts' => -1,
'post_type' => 'my_post_type',
'my-taxo' => 'taxo-1',
'suppress_filters' => false,
'orderby' => 'title',
'order' => 'ASC',
) );
//These can be dynamically generated using any PHP code
if( $cptonomy_arr ) {
foreach ( $cptonomy_arr as $cptonomy_item ) {
$my_cpt_field['choices'][$cptonomy_item->ID] = get_the_title($cptonomy_item->ID); // This populates the select field options fine
// How do I set the 'selected' value'?
}
return $my_cpt_field;
});
My question is, now, how to set the selected option for my field_1234 based on the value already set by _custom_post_type_onomies_relationship. In other words, how to set the selected option in a dynamically populated select field based on a pre-existing meta_key value.
Thanks in advance!
Found out, literally a minute after posting this. Turns out it was a matter of getting the meta_value first, then setting the field via $my_cpt_field[‘default_value’]
Here’s the code
//Populate the Select field, 'My Post Type' (field_1234) with dynamic values
add_filter('acf/load_field/key=field_1234', function($my_cpt_field) {
global $post_id;
$post_id = $_GET['exp_id'];
$cptonomy_post_meta = get_post_meta($post_id,'_custom_post_type_onomies_relationship',false);
$cptonomy_val1 = $cptonomy_post_meta[0];
$cptonomy_val2 = $cptonomy_post_meta[1];
// Cptonomy Loop
$cptonomy_arr = get_posts( array(
'numberposts' => -1,
'post_type' => 'my_post_type',
'my-taxo' => 'taxo-1',
'suppress_filters' => false,
'orderby' => 'title',
'order' => 'ASC',
) );
//These can be dynamically generated using any PHP code
if( $cptonomy_arr ) {
foreach ( $cptonomy_arr as $cptonomy_item ) {
$my_cpt_field['choices'][$cptonomy_item->ID] = get_the_title($cptonomy_item->ID); // This populates the select field options fine
// How do I set the 'selected' value'?
$my_cpt_field['default_value'] = $cptonomy_val2; // this way
}
return $my_cpt_field;
});
Hi!
It’s really old your post but I really would love to be able to make my CPT to act as post and taxonomy at the same time.
Does this still works?
Thanks!
The topic ‘Dynamically Populate Field + CPTonomy’ is closed to new replies.
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.