Home › Forums › Backend Issues (wp-admin) › get_field() empty on save_post when bulk editing
Hi guys
I’ve written a small function that is trigged when saving a a cpt ‘myCPT’
it works nicely in the post editor,
but if Iedit my post from the post list, with the bulk editor, changing post statut for instance, then it delete the terms, and do not re-add them. The relationship is kept, and I can “restore” the terms by saving the post from the editor again.
Do you guys have any clues what’s going on, and how to fix that ?
Thank’s in advance
add_filter( 'save_post', 'change_post_data_before_save', 10, 2 );
//add_filter( 'acf/save_post', 'change_post_data_before_save' ); // nothing happens
function change_post_data_before_save( $post_id, $post ) {
if ( 'myCPT' == $post->post_type ) {
wp_set_object_terms( $post_id , null, 'custom-tax', false );
$related_post = get_field('myRelationshipField');
if($related_post) {
foreach( $related_post as $related) {
$related_id = $related-> ID;
$related_shadow_term_id = (int) get_post_meta( $related_id, 'shadow_term_id', true );
wp_set_object_terms( $post_id , $related_shadow_term_id, 'custom-tax', true );
};
};
};
};
In this context you must supply the post ID to get the field from
$related_post = get_field('myRelationshipField', $post_id);
Hi
Spot-on !
This now seems obvious, thanks
In the meantime, I had found an other way to restrict the execution of my fuction on the post edit page by nesting it into this
global $pagenow;
if (( $pagenow == 'post.php' )) {
//do your stuff
}
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!
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.