Skip Navigation

[Resolved] Assigning parent posts on a CRED form based on Author as current user

This support ticket is created 7 years, 1 month 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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9: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/Hong_Kong (GMT+08:00)

This topic contains 6 replies, has 2 voices.

Last updated by Luo Yang 7 years, 1 month ago.

Assisted by: Luo Yang.

Author
Posts
#571258

Hi guys -

Here’s my use case: I am working on a site dedicated to one hobby. I will let practitioners of that hobby share and recommend resources useful for that hobby.

The minimum structure for such a scenario is:
- a “practitioner” CPT: used by users to display information about them from custom fields and taxonomies linked to this CPT
- a “resource” CPT: used to display resources and additional information from custom fields and taxonomies linked to this CPT
- a “recommendation” CPT: used as an intermediary CPT to implement a many-to-many post relationship between the 2 previous CPTs, so with the 2 previous CPTs set as Parents

Note: I am completely open to replacing the “practitioner” CPT by the wordpress user profile with added custom fields if that makes my use case easier, but only if I can define the layout of the user profile using either a content template, Layout or even Beaver Builder, but I need to have fine-grained control on it, which seems difficult as I understand it.

Here is the context:
- users can register as a wordpress user, and from there create their “practitioner” profile by filling a CRED form with custom fields creating a “practitioner” CPT post
- they can then navigate to a “resource” CPT post on the front-end

Here is what I want to achieve:
- when they are on the “resource” post front-end, they see a button (in my mind, the submit button of a CRED form)
- when they click on that button, which would say something like “Recommend this”, then a CRED form is submitted
- the CRED form would:
- create a “recommendation” CPT post
- assign as parent the “resource” CPT post on which page they currently are and where the CRED form is placed
- assign as parent the “practitioner” CPT post for which the current user is the author (users are not expected to create more than 1 “practitioner” post)

Given that my only real hard needs are to
- let user recommend resources with 1 click
- display a list of the resources recommended by a user on their “practitioner” post
- display a list of the practitioners/users who recommend a resource on each resource page

My questions would be:
- would the general structure of using CPTs like this make sense? If not, I am totally open to better suggestions 🙂
- if yes, would you have any pointers as to how I could structure the CRED form on the “resources” post to automatically assign the correct parent posts in the form?

I have searched extensively through the forum but couldn’t find any info on that specific use case 🙁

Many thanks!

Best,
- Julien

#571308

Dear Julien,

Thanks for the details.

For your question: would the general structure of using CPTs like this make sense?

Yes, in your case, I think the many-to-many relationships you mentioned above is the best option,

For this question:
have any pointers as to how I could structure the CRED form on the “resources” post to automatically assign the correct parent posts in the form?

If you are using CRED shortcode [cred_field] to display the parent posts select field, you can specific the default value as the parent post ID, see our document:
https://toolset.com/documentation/user-guides/cred-shortcodes/#cred_field
Argument "value". Optional. Preset value

#571324

Hi Luo -

Many thanks for this and for confirming the general approch!

I think I know how to get the "resource" post ID, as this form would be displayed on the "resource" post.

However, I'm just unsure how I would go about getting the "practitioner" post ID dynamically to insert it into the CRED form. The only info I see that I can use is that this is the only "practitioner" post for the logged-in user. But I don; t know how to exploit this to insert into the CRED form 🙁

Many thanks!

Best,
- Julien

#571621

In your case, I suggest you try with CRED action hook "cred_save_data" to achieve what you want, for example, when user submit the CRED form, use action hook "cred_save_data" to trigger a custom PHP function, in this PHP function do these:
1) Get the current user ID:
https://developer.wordpress.org/reference/functions/get_current_user_id/
2) Use the user ID to get the related "practitioner" ID,
https://developer.wordpress.org/reference/functions/get_post/
3) Types plugin is using a custom field _wpcf_belongs_[post-type-slug]_id to store the parent post ID, so you can use "practitioner" ID to update the custom field value of "_wpcf_belongs_practitioner_id"

More help:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
cred_save_data
This hook allows doing a custom action when post data is saved to database.

#572404

Hi Luo -

I finally got this working, but with a difference approach. I wanted to share my solution for my use case so it can assist others if needed.

First, since my "practitioner" CPT has only one post per user and since getting the post ID proved quite difficult, I adopted a workaround: when users create their "practitioner" post, I use a function to record the post ID as a user custom field using the following code:

// CRED form action to save the practitioner ID to the user custom field when a profile is created or edited
add_action('cred_save_data_28', 'jln_add_practitoner_id_create',10,2);
function jln_add_practitoner_id_create($post_id, $form_data)
{
$user_id = get_current_user_id();
update_user_meta($user_id, 'wpcf-practitioner-profile-post-id', $post_id);
}

Second, I am using the following CRED form code as my "Recommend" button:

[credform class='cred-form cred-keep-original']

	[cred_field field='form_messages' value='' class='alert alert-warning']

	<div class="form-group" hidden>
		<label>Recommendation Book Name</label>
		[cred_field field='post_title' post='reco-book' value='[wpv-user field="user_firstname"] [wpv-user field="user_lastname"] recommends [wpv-post-title]' urlparam='' class='form-control' output='bootstrap']
	</div>

	<div class="form-group" hidden>
		<label>Recommendation Book Description</label>
		[cred_field field='post_content' post='reco-book' value='' urlparam='' output='bootstrap']
	</div>

	<div class="form-group" hidden>
		<label>practitioner Parent</label>
		[cred_field field='_wpcf_belongs_practitioner_id' value='[types usermeta='practitioner-profile-post-id' user_is_author='true' output='raw'][/types]' select_text='--- not set ---' class='form-control' output='bootstrap']
	</div>
	<div class="form-group" hidden>
		<label>book Parent</label>
		[cred_field field='_wpcf_belongs_book_id' value='[wpv-post-id]' select_text='--- not set ---' class='form-control' output='bootstrap']
	</div>
	[cred_field field='form_submit' value='Recommend' urlparam='' class='btn btn-primary btn-lg' output='bootstrap']

[/credform]

This populates correctly both parent posts IDs.

Third, I am using the following function on form submit to keep the user on the same page when the form is submitted, so that I get the behavior of a button:

// CRED Redirect to current Book page on Book Recommendation submissions
add_filter('cred_success_redirect_160', 'redirect_current_page_160', 10, 3);
function redirect_current_page_160( $url, $post_id, $form_data )
{
return $_SERVER;
}

That's it. I still need to deal with conditional display if the user has recommended the page ro not, as well as with an action to "un-recommend" it, but at least I got this working for now 🙂

#572557

Hi Luo, just a quick follow-up question on this: do you see the upcoming changes to the post relationships structure impacting this in any way?

Many thanks!

Best,
- Julien

#573118

Thanks for share the solutions, that will help other users.
And for the question:
There isn't any upcoming changes on parent/child post type relationships, but there will be some change in many-to-many relationships:
https://toolset.com/2017/09/preview-for-many-to-many-relationships-in-toolset/

And we will offer the tools to transfer the current many-to-many relationship to the new many-to-many relationship, so it is safe to use current many-to-many relationship post type structure.

This ticket is now closed. If you're a Toolset client and need related help, please open a new support ticket.