Well, I guess I figured it out. However, would doing this even be advisable?
I ask because… if we’re be able to manage the post_excerpt
via acf/save_post
with a custom ACF WYSIWG field, wouldn’t it just bloat the database? I.e. have a value stored for both the ACF and post_excerpt
field?
The code I’ve used:
function update_post_excerpt($post_id) {
// Set excerpt value from field
$post_excerpt = get_field('field_12345',$post_id);
// Update post options array
$update_post_excerpt_args = array(
'ID' => $post_id,
'post_excerpt' => $post_excerpt,
);
// Update the post into the database
wp_update_post( $update_post_excerpt_args );
}
add_action('acf/save_post', 'update_post_excerpt', 20);
Hi James,
Thanks for your reply. Unfortunately I’ve not yet tried theacf/save_post hook on this. I’m not entirely sure how to accomplish this and generally have difficulty with understanding this hook.
Best,
Ryan
Ok, I’ve figured out the author bit, because I needed to use the ID. I’m still stuck on the taxonomy
$options = array(
'post_id' => 'new_post',
'post_title' => true,
'new_post' => array(
'post_type' => 'my-cpt',
'post_status' => 'publish',
'post_author' => $user_id // Where $user_id is a number
'tax_input' => array('my_taxonomy' => $taxo_id), // Not working
),
'field_groups' => array(
1234
),
'submit_value' => __("Add CPT", 'acf'),
'updated_message' => __("CPT Added", 'acf'),
'uploader' => 'wp'
);
acf_form($options);
Regarding the taxonomy, I’ve read this thread:
acf_form, new_post, tax_input requires to be logged in
– and –
Using ‘tax_input’ with wp_insert_post() and nothing happens
…so it seems that the user needs permission to assign taxonomies – OR – I need to use wp_set_object_terms()
using acf/save_post
. I’ve increased permissions, which appears to have worked, but I am curious if there’s a way to do this via wp_set_object_terms()
so I don’t have to increase permissions.
Does anyone have any ideas?
Thanks in advance!
You’re a rock star John! Thank you so much, that worked perfectly!
Hey John!
Thanks for the response!
RE: Database Values The value in question uses separate database rows.
You’re right about getting the data from the other plugin (the plugin is CPT-onomies, https://wordpress.org/plugins/cpt-onomies/) being relatively easy and I’m able to do this.
RE: Type of ACF Field
I’m using a select that I’m populating dynamically. Ideally each one of these selects would correspond to a different row in the database meta_key value.
Hope this makes sense and I’m happy to clarify with anything else if need be!
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;
});
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.