Skip Navigation

[Resolved] Populate CRED form with 2 parents

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

Problem:
How to display CRED form with parent post of current logged in user and add two parents

Solution:
To display CRED parent field which displays only current logged in user posts, you need to following the steps given in the following reply:

You can find the proposed solution with the following reply:
https://toolset.com/forums/topic/populate-cred-form-with-2-parents/#post-404588

Relevant Documentation:

This support ticket is created 7 years, 9 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
- 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 24 replies, has 2 voices.

Last updated by Geoffrey Cleverley 7 years, 9 months ago.

Assisted by: Minesh.

Author
Posts
#404510

I am creating a Book review membership site.

I have the custom posts Books, Members, and Reviews.

Users must create only one Member post. Members and Books posts are parents of Reviews posts.

When a user clicks to review a book, they are taken to a CRED form that autopopulates the parent dropdown for the book. This is great because we could have thousands of books and the dropdown parent selector wouldn't provide an optimal user experience for this.

However there is a problem with regards to the backend workflow for the admins.

I want the CRED form to auto-populate the Member post parent dropdown on the form too.

Is there a way for it to show only the member posts authored by the currently logged in user? (given there will be only one)

I understand from reading similar older support threads that nesting shortcodes is no longer workable.

At the moment I have included the Parent Member post dropdown in the CRED form and directions for the users to make sure they register the review with their 'Member' post.

This is less than optimum, as the user base grows, this will be more resource intensive. Okay the users can just type to find their user name, but populating the list is still cumbersome and slow.

It also allows users to mistakenly post their reviews to other people's member profile posts.

I have warned that users that don't populate this dropdown will have reviews deleted, otherwise the problem will fall to the admins needing to link the posts to their parents from the backend.

Any solutions?

Thanks

Jeff

#404588

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Could you please try to follow following steps:

1. In CRED "[child] Post Form"

2. In the Parent Selector Code of that form you should use only default available arguments, as this:

<div class="cred-group cred-group-parents">
<div class="cred-field cred-field-_wpcf_belongs_page_id">//change "page" to your parent post slug
<label class="cred-label">
Choose Parent
</label>
<input type="hidden" id="parents_id" value="[get-parents]" /> This is only for test display: [get-parents]
[cred_field field='_wpcf_belongs_page_id' value='']//change "page" to your Parent Post Slug
</div>
</div>

3. In the CRED JS Box add following code:

jQuery(document).ready(function(){
    
var post_parents = jQuery('#parents_id').val();
    
var arr = post_parents.split(',');
      
  jQuery("[name=_wpcf_belongs_page_id] > option").each(function() { //change "page" to your parent post slug
        
    var option_val = jQuery(this).val();
        
     if( jQuery.inArray(option_val, arr) == -1 && option_val != -1 ){
       jQuery(this).remove();
     }
  });
      
});

4. In your current theme's functions.php - add following code:

function get_parents($atts) {
    global $current_user;
    get_currentuserinfo();
    $author_query = array('post_type' => 'page', 'posts_per_page' => '-1','author' => $current_user->ID,);   /// change "page" with your parent post_type slug
    $author_posts = new WP_Query($author_query);
    $parent_ids = "";
    while($author_posts->have_posts()) : $author_posts->the_post();
        $parent_ids .= get_the_ID() .",";
    endwhile;
    
    return $parent_ids;
}
add_shortcode('get-parents', 'get_parents');

5. in Views > Compatibility > 3rd party ShortCodes register:

get-parents

6. If you insert the CRED Form in a Post, it correctly displays only parent Posts of current author/user.

I hope above solution will help you to resolve your issue.

#404832

Thank you so much Minesh, that is great.

I am consistently astonished at the help and experience of the support staff at OnTheGoSystems, and you my sir are no exception. Really wonderful support, much appreciated.

The Member parent selector now only displays the posts of the current author.

The field defaulted to 'No Parent', so I made it a required field, so that a user can't submit a review without it being assigned to their member profile.

However, is it possible for the field to be autopopulated and perhaps even hidden? If possible I would love to remove or lower the steps a user must take.

I know I ask a lot Minesh, and I am incredibly grateful for your support.

Jeff

#404966

Minesh
Supporter

Languages: English (English )

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

Then you can create a generic hidden field and use "cred_before_save_data" CRED hook to assign the value to hidden field.

You can know more about on the following link where I've describe solution. Please make sure that You need to give generic hidden field name as: "_wpcf_belongs_page_id" (change page with your parent post slug)

You can find such kind of solution on the following reply:
https://toolset.com/forums/topic/notification-of-author-of-a-parent-post-when-a-child-post-is-created/#post-364722

I hope this will help you to resolve your issue.

#404983

Thanks for the link and pointer Minesh.

I'll give it a go and get back to you with my results.

Great work

Jeff

#405030

Minesh
Supporter

Languages: English (English )

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

Sure - take your time and I hope it will help you to resolve your issue.

#405155

I need a little more help.

In my CRED form, in the form content field I previously had:

<div class="cred-field cred-field-_wpcf_belongs_member_id">
			<label class="cred-label">
              <h3>Reviewed by:</h3>
		</label>
          <input type="hidden" id="parents_id" value="[get-parents]" /> 
          	[cred_field field='_wpcf_belongs_member_id' value='' order='date' ordering='desc' required='true' select_text='--- You Must Select Your Profile---' validate_text='member must be selected']
		</div>

.....with this in the JS field:

jQuery(document).ready(function(){
     
var post_parents = jQuery('#parents_id').val();
     
var arr = post_parents.split(',');
       
  jQuery("[name=_wpcf_belongs_member_id] > option").each(function() { //change "page" to your parent post slug
         
    var option_val = jQuery(this).val();
         
     if( jQuery.inArray(option_val, arr) == -1 && option_val != -1 ){
       jQuery(this).remove();
     }
  });
       
});

and the following in my functions.php:

/*CRED form only populate book review member parent field with current authors member*/
function get_parents($atts) {
    global $current_user;
    get_currentuserinfo();
    $author_query = array('post_type' => 'member', 'posts_per_page' => '-1','author' => $current_user->ID,);   /// change "page" with your parent post_type slug
    $author_posts = new WP_Query($author_query);
    $parent_ids = "";
    while($author_posts->have_posts()) : $author_posts->the_post();
        $parent_ids .= get_the_ID() .",";
    endwhile;
     
    return $parent_ids;
}
add_shortcode('get-parents', 'get_parents');

With the following shortcode registered in views:

get-parents

But now that I want the field to be auto hidden and prepopulated with the parent post [get-parent], I need to use the "cred_before_save_data" hook (https://toolset.com/documentation/user-guides/cred-api/#cbsd)

So not in my form field I should replace the above with:

<div class="cred-field cred-field-_wpcf_belongs_member_id">

          <input type="hidden" id="parents_id" value="[get-parents]" /> 
          	[cred_generic_field field="_wpcf_belongs_member_id" type="hidden" class="" urlparam=""]
{
"required":0,
"validate_format":0,
"persist":1,
"default":""
}
[/cred_generic_field]
		</div>

and in the functions I need to use the "cred_save_before_date" hook in addition with the previous function and javascript. So in addition to the above I add something like:

add_action('cred_before_save_data', 'my_before_save_data_action',10,1);
function my_before_save_data_action($form_data)
{
    // if a specific form
    if ($form_data['id']==250)
    {
        if(isset($_POST['_wpcf_belongs_member_id']) and $_POST['_wpcf_belongs_member_id']!=""){
            $parent_post_id = $_POST['_wpcf_belongs_member_id'];
            $x = get_post_meta($parent_post_id,'_wpcf_belongs_member_id',true);
            $_POST['get_parents'] = $x;
             
        }
    }
};

Unfortunately my php is very limited and I have been fiddling around with variants of the above for a while to no avail.

Hopefully you can explain a little bit more for me?

Thanks Minesh

Jeff

#405161

Minesh
Supporter

Languages: English (English )

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

Now I need to know your data structure in order to help you in right direction.

I understand you want to have hidden field that should contain the "_wpcf_belongs_member_id" parent id value.

Could you please explain your data structure and its post type relationships with example and which ID you would like to store are parent member id?

#405291

Hi Minesh for the membership book archive review site I have created 4 CPT's, Books (slug: book), Authors (slug: book-authors), Publishers (slug: publisher), Members (slug: member) and Reviews (slug: review).

The Book posts are children of Publisher and Author posts, while the Review posts are children of the Member and Book posts.

Child-parent structure of the reviews with books and members is functional here. I followed the Toolset guide (with some support) for setting up the review system.

When a user is on a Book post page, there is a link to a page with the CRED post book review form (Form ID=250) which autopopulates the CRED form with the book parent via the url parameter garnered from the post which included said link.
(support thread: https://toolset.com/forums/topic/membership-based-review-site-user-profiles-products-and-reviews/#post-398672)

So my CRED review form autopopulates the Book parent of the review, and with the instructions you gave me previously the Member parent selector only contains the Member post of the currently logged in author.

So I understand with the "cred_save_before_action" hook I can autopopulate the other Member parent selector field (which I can also hide) prior to the forms submission, which effectively autoselects the member parents without a users interaction, thus assigning the review post to it's correct Member profile and providing a smoother user experience.

Thanks again for your support Minesh

Jeff

#405320

Minesh
Supporter

Languages: English (English )

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

Could you please try following code. This will poupulate generic field with your member id value so you will not have to assign it using "cred_save_before_action".

[cred_generic_field field="_wpcf_belongs_member_id" type="hidden" class="" urlparam=""]
{
"required":0,
"validate_format":0,
"persist":1,
"default":"[wpv-post-field name='_wpcf_belongs_member_id']"
}
[/cred_generic_field]

I hope this will help you to resolve your issue.

#405394

Hi Minesh,

I actually elevated your user account to an admin, incase you wanted to have a look in.

#405424

Minesh
Supporter

Languages: English (English )

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

Could you please share problem url and 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.

#405553

Minesh
Supporter

Languages: English (English )

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

Sorry Jeff, but unfortunately I do not have wp-admin access details yet, I can not see in the private reply. Could you please resend it and your CRED form URL.

#405572

Minesh
Supporter

Languages: English (English )

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

Yes - thanks for the info. Now I can login to front section and see the CRED form.

But still - unfortunately I could not able to access "wp-admin", could you please give full rights to this newly created user so that I should be able to access wp-admin.

#405576

Sorry, I'm in Shanghai and sometimes my connection gets stuck mid-loading with my VPN. The elevated privileges hadn't saved. It should be good to go now.

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