Skip Navigation

[Resolved] Cred Form associating with CPT

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

Problem:
How to associate parent post ID when you create child posts using CRED form.

Solution:
You can add CRED generic hidden field to hold the parent ID of child post and build custom shortcode that return the desired parent id in this case.

You can find the proposed solution in this case with the following reply:
=> https://toolset.com/forums/topic/cred-form-associating-with-cpt/#post-593373

Relevant Documentation:
=> https://toolset.com/documentation/user-guides/inserting-generic-fields-into-forms/

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
- 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)

This topic contains 6 replies, has 2 voices.

Last updated by davidL-10 7 years ago.

Assisted by: Minesh.

Author
Posts
#592457
Edit Testimonial.png
Edit Member Profile ‹ Wedding Band Association — WordPress (1).png

Hi there,

Can you give me some more direction, please?

The situation is this: hidden link

This is a Directory of Wedding Bands.
(CPT = wedding-bands)
Bands will have a Biog, Band Logo, videos, all as post fields of the CPT.

Then we have Testimonials.
These are a child post type of 'wedding-bands".
Title, Content, Name of the person leaving the review and 5-star rating.
These can only be entered by the band admin. Not the public.

So,
Band Admins register as a custom User Role (Band Admin)
During the registration via the Cred Form, we worked with previously (Christian provided a Function), You helped me create a Custom Post Type for the Band when a user registers.

So the user is registered and the band Name FIeld in the Form is used to create a new CPT in the wedding bands CPT.

So far, All Good.

Now, the problem I am having is with Testimonials.
I have a Cred Form to Add Testimonials But I cannot associate it with the band.

I am using a page for the admin to add/edit testimonials called
hidden link

It contains a view called "testimonials-back-end"

In that View, I filter Testimonials like this "Select posts with the author the same as the current logged in user."

It shows testimonials and offers a link to a Layout to edit existing ones,
and a form to add new ones.
[cred_form form="testimonials"]

I am trying to get this form to tie new testimonials to the Wedding band.
I don't want a drop down for members to select their own band.

Is there a way I can get the Cred Form to know what band is relevant based on the User who is logged in.
I would like the testimonials to show - This Testimonial Belongs to Member Profile { the author's band }

I hope the screenshots make this easier to understand.

#592512

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

That means, we need to find "wedding-bands" post's ID based on currently logged in user ID and the "wedding-bands" post ID we find that should be setup as parent of testimonial - correct?

#592842

Yes, exactly.

Thanks for your reply.
David

#592899

Minesh
Supporter

Languages: English (English )

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

So, could you please share from where access details with one test case setup of user + "wedding-bands" post and share me access details.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I would additionally need your permission to de- and re-activate Plugins and the Theme, and to change configurations on the site. This is also a reason the backup is really important. If you agree to this, please use the form fields I have enabled below to provide temporary access details (wp-admin and FTP).

I have set the next reply to private which means only you and I have access to it.

#593369

Minesh
Supporter

Languages: English (English )

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

Well - I tried to add the following code to your current theme's functions.php file but strangely its throwing error, I dont know why.

function func_get_wedding_bands_author_post_id()
{
global $current_user;

$args = array(
    'author' => $current_user->ID,
    'post_type' => 'wedding-bands');
$author_posts = new WP_Query( $args );

//if(count(($author_posts->posts) > 0){
	return $author_posts->post->ID;
//}

}
add_shortcode('get_wedding_bands_author_post_id','func_get_wedding_bands_author_post_id' );

What you should do is, query your post type "wedding-bands" by current logged in user and return the post ID you found.

More info:
https://wordpress.stackexchange.com/questions/109710/get-posts-get-all-posts-by-author-id

Edit your CRED form and add following field:

[cred_generic_field field='_wpcf_belongs_wedding-bands_id' type='hidden' class='' urlparam='']
{
"required":0,
"validate_format":0,
"default":"1111"
}
[/cred_generic_field]

Where:
Replace "1111" with the post id you found using above method.

#593373

Minesh
Supporter

Languages: English (English )

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

Ok - looks like there was internet issue and file not saved successfully.

I've added following code to your current theme's functions.php file:

function func_get_wedding_bands_author_post($atts, $content = '') {

global $current_user;
 
$args = array(
    'author' => $current_user->ID,
    'post_type' => 'wedding-bands');
$my_author_posts = new WP_Query( $args );

return $my_author_posts->post->ID;

}

I've added following code to your CRED form:

[cred_generic_field field='_wpcf_belongs_wedding-bands_id' type='hidden' class='' urlparam='']
{
"required":0,
"validate_format":0,
"default":"[get_wedding_bands_author_post_id]"
}
[/cred_generic_field]

Now, once you create testimonial, you will see it will be automatically attached to it's associated parent band profile. Could you please confirm.

#593887

This is amazing,
Thank you Minesh.

This will bring us to the finish of this project.
Thank you very much,

Kind regards

David