Home › Forums › General Issues › Get taxonomies values in relationship field and update with save_post
I need help because I’m freaking out.
I have a relationship field, from woocommerce products I take a custom post type called SERIE, so the ralationship is product and serie.
Serie has more taxnoomies fields, and its are the attributes of woocommerce.
I need 2 solutions and only one work for the moment.
CASE 1 : inherit taxonomies when save a product
When I create a new product, I connect the product with a SERIE throught a relationship field and when I save post, the new product inherit the taxomies of SERIE.
So I developed a function in functions.php and it works!
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 WITH RELATIONSHIP FIELD
$posts = get_field('serie_di_appartenenza');
if( $posts ):
foreach( $posts as $p ):
//FIRST TAXONOMY
$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);
}
}
CASE 2 : update taxonomies info of products when I update the serie custom post
Foe example, I’ve already created the custom post SERIE and I want change the value of taxonomies. When update the post I want update all taxonomies of products connectes with relationship field.
I developed this function in functions.php and it doesn’t work !
add_action( 'acf/save_post', 'aggiorno_attributi_prodotti_correlati' , 1);
function aggiorno_attributi_prodotti_correlati( $post_id ) {
$post_type = get_post_type($post_id);
global $product;
if ( 'serie_prodotti' == $post_type ) {
global $product;
// get products connected
$doctors = get_posts(array(
'post_type' => 'product',
'posts_per_page' => '-1',
'meta_query' => array(
array(
'key' => 'serie_di_appartenenza',
'value' => '"' . get_the_ID() . '"',
'compare' => 'LIKE'
)
)
));
if( $doctors ):
foreach( $doctors as $doctor ):
//TAKE RELATED INFO IN POST TYPE WITH RELATIONSHIP FIELD
$posts = get_field('serie_di_appartenenza');
if( $posts ):
foreach( $posts as $p ):
//FIRST TAXONOMY
$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( $doctor->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( $doctor->ID, '_product_attributes',$data);
endforeach;
endif;
}
}
Help please 🙂
Any suggestions ??
Thanks
The topic ‘Get taxonomies values in relationship field and update with save_post’ 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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.