Home › Forums › General Issues › Manipulate custom fields (created in acf) in a function with save_post action (o › Reply To: Manipulate custom fields (created in acf) in a function with save_post action (o
@hube2
Solved with your help, today I did the function I needed! Thanks!
I write a piece, maybe someone can help.
The function takes the title of a post-type related to products (woocommerce) and inserts this title as a woocommerce attribute when save_post action !
add_action('acf/save_post', 'update_post_meta', 20);
function update_post_meta( $post_id ) {
$post_type = get_post_type($post_id);
global $product;
if ( 'product' == $post_type ) {
//TAKE RELATED INFO IN POST TYPE
$posts = get_field('serie_di_appartenenza');
if( $posts ):
foreach( $posts as $p ):
$scrittore=get_field("scrittore_appartenenza", $p->ID);
if( $posts ):
foreach( $scrittore as $s ):
$scrittore_ereditato= get_the_title( $s->ID );
//ADD INFO IN GLOBAL ATTRIBUTE WOOCOMMERCE
$term_taxonomy_ids = wp_set_object_terms( $post_id, $scrittore_ereditato, 'pa_scrittore-woo', true );
endforeach;
endif;
endforeach;
endif;
//ADD INFO IN CURRENT POST
$data = Array(
'pa_scrittore-woo'=>Array(
'name'=>'pa_scrittore-woo',
'value'=>$scrittore_ereditato,
'is_visible' => '1',
'is_variation' => '1',
'is_taxonomy' => '1'
),
);
//UPDATE META
update_post_meta( $post_id, '_product_attributes',$data);
}
}
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.