Skip Navigation

[Resolved] Getting parent post ID sent to user custom field upon Post CRED form submission

This thread is resolved. Here is a description of the problem and solution.

Problem:
2 CPTs: book, reco-book and book is parent of reco-book
1 CRED form to create a new reco-book positioned on the book post
I am trying to use cred_save_data to record the book parent post id of the reco-book post created through the form into a repeating custom user field named recommended-book-ids.

Solution:
Please use this updated code:

add_action('cred_save_data_160', 'jln_add_book_id',10,2);
function jln_add_book_id($post_id, $form_data)
{
$user_id = get_current_user_id();
$parent_id = get_post_meta($post_id, '_wpcf_belongs_book_id', true);
add_user_meta($user_id, 'wpcf-recommended-book-ids', $parent_id);
}
This support ticket is created 7 years, 2 months ago. 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
- 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 -
- 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 2 replies, has 2 voices.

Last updated by julienL-5 7 years, 2 months ago.

Assisted by: Noman.

Author
Posts
#572407

Please note this is a different problem but a follow-up on this thread: https://toolset.com/forums/topic/assigning-parent-posts-on-a-cred-form-based-on-author-as-current-user/

Context:
2 CPTs: book, reco-book and book is parent of reco-book
1 CRED form to create a new reco-book positioned on the book post

I am trying to use cred_save_data to record the book parent post id of the reco-book post created through the form into a repeating custom user field named recommended-book-ids.

Here is the code I am using:

add_action('cred_save_data_160', 'jln_add_book_id',10,2);
function jln_add_book_id($post_id, $form_data)
{
$user_id = get_current_user_id();
$parent_id = get_post_meta($post_id, '_wpcf_belongs_book_id');
add_user_meta($user_id, 'wpcf-recommended-book-ids', $parent_id);
}
?>

Every time I just get a blank field created and I can't figure out what is wrong 🙁 I have tried to use $post_id directly instead of the $parent_id variable defined here, which works but of course records the reco-book id instead of the book id.

Would you be able to point out where I am wrong?

Many thanks!

Best,
- Julien

#572458

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Julien,

Thank you for contacting Toolset support. Please remove the code you have added, and use this updated code:

add_action('cred_save_data_160', 'jln_add_book_id',10,2);
function jln_add_book_id($post_id, $form_data)
{
$user_id = get_current_user_id();
$parent_id = get_post_meta($post_id, '_wpcf_belongs_book_id', true);
add_user_meta($user_id, 'wpcf-recommended-book-ids', $parent_id);
}

If still issue persists, then I need to request temporary access (WP-Admin and FTP Login info) to your staging site.

Your next answer will be private which means only you and I have access to it.

=== Please backup your database and website ===

✙ I would additionally need your permission to de-activate and re-activate Plugins and the Theme, and to change configurations on the site. This is also a reason the backup is really important.

✙ Please add the Links to the [CRED Form] Edit Screen and [Page] Edit Screen where you have inserted this Form.

Thankyou

#572555

Many thanks Noman, that's working perfectly 🙂 I thought the "true" was optional but I was obviously wrong 🙂

Many thanks!

Best,
- Julien