Home Forums Backend Issues (wp-admin) Update a field when duplicating a post


Update a field when duplicating a post

  • Hello,

    I’m using Woocommerce to duplicate Products and duplicating will be the preferred way for the user to create new products, since many custom fields will likely have the same values as existing products.

    Additionally, I have a field group called Product Readiness with a single required True/False field whose label asks “Are you ready to publish?”. This field group is set to display on Product Pages that don’t have a Post Status of Published. So the goal here, when a product is duplicated, is to encourage the user to make sure they’ve made all changes necessary before publishing.

    I just added this field/group after creating a few products. So if I duplicate one of them, the default value is no, and you have to switch to yes (as desired) to publish the post. However, if I duplicate a product that was originally a duplicate itself, then the value is set to yes, because the value was stored when the first duplicate was published.

    So I’m thinking that I need to update the field’s value to reset it to the default using update_field() when the product is duplicated. But I’m not really sure how to check for when a product is being duplicated. Can anyone demonstrate how I would add this functionality to my child theme’s functions file?

    Thank you!

  • Ok, I figured out that this could be achieved by resetting the value to false (no) after a product is published. That way the duplicate is loaded with No selected.

    This code works, but please let me know if I can improve upon it. Thanks.

    function reset_product_readiness_after_publish( $post_id ){  
        $value = "0";
        update_field('field_30705432c7518', $value, $post_id);
    add_action('acf/save_post', 'reset_product_readiness_after_publish', 20);
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.