ACF auto complete all value from 1 cpt to another cpt

To automatically populate the values of CPT 2 fields from CPT 1 when their field names match, you can achieve this using Advanced Custom Fields (ACF) and some custom code. Here’s a step-by-step guide on how to do it:

  1. Install and Activate ACF: Make sure you have ACF installed and activated on your WordPress site.
  2. Create Fields in CPT 1 and CPT 2: Create the necessary custom fields in both CPT 1 and CPT 2 as described in your question.
  3. Write Custom PHP Code:
function populate_cpt2_fields_from_cpt1($post_id) {
    if (get_post_type($post_id) === 'cpt1') {
        // Get the current CPT 1 post
        $cpt1_post = get_post($post_id);

        // Get the field values of CPT 1
        $field1_value = get_field('field_1', $cpt1_post);
        $field2_value = get_field('field_2', $cpt1_post);
        $field3_value = get_field('field_3', $cpt1_post);
        $field4_value = get_field('field_4', $cpt1_post);
        $field5_value = get_field('field_5', $cpt1_post);

        // Get the corresponding CPT 2 post
        $cpt2_post = get_page_by_title($cpt1_post->post_title, OBJECT, 'cpt2');

        if ($cpt2_post) {
            // Update the fields in CPT 2 if they exist and match CPT 1 field names
            update_field('field_1', $field1_value, $cpt2_post->ID);
            update_field('field_2', $field2_value, $cpt2_post->ID);
            update_field('field_3', $field3_value, $cpt2_post->ID);
            update_field('field_4', $field4_value, $cpt2_post->ID);
            update_field('field_5', $field5_value, $cpt2_post->ID);
        }
    }
}

add_action('acf/save_post', 'populate_cpt2_fields_from_cpt1', 20);

Replace 'cpt1' and 'cpt2' with the actual slugs of your CPT 1 and CPT 2.

  1. How It Works:

    • The code above hooks into the ACF acf/save_post action.
    • It checks if the saved post is of type CPT 1.
    • If it is, it retrieves the field values of CPT 1.
    • It then looks for a CPT 2 post with the same title as CPT 1 (assuming titles match).
    • If a CPT 2 post is found, it updates the fields in CPT 2 with the values from CPT 1, but only if the field names match.
  2. Usage:

    • Whenever you create or update a CPT 1 post, the code will automatically populate the corresponding fields in CPT 2 if they exist and have matching field names.

Remember to test this code on a development/staging site first to ensure it works as expected with your specific setup. You can test the code in functions.php within your theme. Also make sure to adjust the CPT slugs and field names to match your actual configuration.

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)