Navigation überspringen

[Gelöst] Edit user custom field when add a relationship cpt

This support ticket is created vor 1 Jahr, 8 Monaten. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

Dieses Thema enthält 7 Antworten, hat 2 Stimmen.

Zuletzt aktualisiert von claudioM-3 vor 1 Jahr, 8 Monaten.

Assistiert von: Minesh.

Author
Artikel
#2597651

Tell us what you are trying to do?
I have a user cpt "aa" (each user have only one "aa"). Cpt "aa" are relate many to 1 on cpt "bb". I wish that when a user with relationship form add a relationship with own "aa" cpt with a specific cpt "bb" his custom user filed "titlebb" was edit with that specific cpt "bb" title.

Is there any documentation that you are following?
I try with cred_save_data

function save_bb_title_to_user_meta( $rel_id, $form_data ) {
    $user_id = get_current_user_id();
    $aa_post_id = get_user_meta( $user_id, 'aa', true );
    $bb_posts = types_child_posts( 'aa-bb', $aa_post_id );
    if ( ! empty( $bb_posts ) ) {
        $bb_post = $bb_posts[0];
        $bb_title = get_the_title( $bb_post->ID );
        update_user_meta( $user_id, 'wpcf-titlebb', $bb_title );
    }
}

add_action( 'cred_saved_data', 'save_bb_title_to_user_meta', 10, 2 );

Thanks

#2597687

Minesh
Supporter

Sprachen: Englisch (English )

Zeitzone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

The hook "cred_save_data" should be used with the post or user type forms.

Are you using post relationship forms on frontend?

#2597717

Yes

#2597721

Minesh
Supporter

Sprachen: Englisch (English )

Zeitzone: Asia/Kolkata (GMT+05:30)

There is no hook available to update the post relationship entity on fly.

Maybe you can use the following guidance that may help you as a workaround:
- https://toolset.com/forums/topic/save-post-a-custom-field-value-to-relationship-custom-fields-based-on-post-b-val/#post-1180894

#2597735

Mmmm no with that way i lost select2 for exemple..
Is it possible have a function that run once 24h that copy a custom field from intermediary post and paste on custom user field?

#2597813

Minesh
Supporter

Sprachen: Englisch (English )

Zeitzone: Asia/Kolkata (GMT+05:30)

For such case you will have to setup a cron-job and for that you should install the addon plugin that offer that feature and add your desired custom code that fulfill the job/task you want.

Here is the article that may help you:
- versteckter Link
- versteckter Link

OR

You may want to contact any of our certified partners for any custom programming requirement:
- https://toolset.com/contractors/

#2597829

When a user create a m2m relation is created a intermediary post with author the user. I set a custom field that i populate in the relationship form called "cc".

If i set a login hook in witch i get a intermediary post own legged user and get a "cc" value and add it on wpcf-titlebb (custom user field)?

add_action('wp_login', 'copia_dati');

function copia_dati($user_login) {
    $user_id = get_user_by('login', $user_login)->ID;
 
    $args = array(
        'post_type' => 'aa-bb',
        'author' => $user_id,
        'posts_per_page' => 1,
    );
    $query = new WP_Query($args);
    if ($query->have_posts()) {
        $query->the_post();
 
        $test2_value = get_post_meta(get_the_ID(), 'wpcf-cc', true);
 
        update_user_meta($user_id, 'wpcf-titlebb', $test2_value);
    }
    wp_reset_postdata();
}

But not work

#2597835

My issue is resolved now. Thank you!