• Resolved Em

    (@malmomen)


    Based on code from ACF support, I have a function to populate an ACF category that works great on the backend, but not when I upload a product with ACF Frontend Form for Elementor. Perhaps “transition_post_status” action does not fire on ACF Frontend Form for Elementor?

    Can you advise me if you have an action that I can hook to on form-submit and then make use of the same logic below to update the category?

    My function:

    function add_this_to_new_products( $new_status, $old_status, $post ) {
     
        global $post;
     
        if ( $post->post_type !== 'product' ) return;
     
        if ( 'publish' !== $new_status or 'publish' === $old_status ) return;
     
        $user = wp_get_current_user();
    
        $categoryid = get_field('category_id', 'user_'.$user->ID );
    
        if($categoryid){
           //add the category here
           wp_set_object_terms( $post->ID, $categoryid, 'product_cat', true );
        }
     
    }
     
    add_action( 'transition_post_status', 'add_this_to_new_products', 10, 3 );

    Thank you

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Shabti Kaplan

    (@shabti)

    I am actually using an ACF function to submit the form called “acf->check_submit_form()” so this question should be directed to them.

    All the best, Shabti

    Thread Starter Em

    (@malmomen)

    Thanks @shabti,
    I removed global $post; and now it partly works on the backend. It creates a new category but doesn’t actually add the product to the category. So I’m wondering if transition_post_status is the right action to hook to?

    function add_this_to_new_products( $new_status, $old_status, $post ) {
     
        if ( $post->post_type !== 'product' ) return;
     
        if ( 'publish' !== $new_status or 'publish' === $old_status ) return;
     
        $user = wp_get_current_user();
    
        $categoryid = get_field('category_id', 'user_'.$user->ID );
    
        if($categoryid){
           //add the category here
           wp_set_object_terms( $post->ID, $categoryid, 'product_cat', true );
        }
     
    }
     
    add_action( 'transition_post_status', 'add_this_to_new_products', 10, 3 );
Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘ACF Default Not Working’ is closed to new replies.