Skip Navigation

[Resolved] Part 2 of Populate CRED form with 2 parents – Thought resolved but isn't.

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

Problem:
How to set default value for dropdown using jQuery and make it disable.

Solution:
You can use jQuery selector and attribute selected to make dropdown select option selected.

You can find proposed solution with the following reply:
https://toolset.com/forums/topic/part-2-of-populate-cred-form-with-2-parents-thought-resolved-but-isnt/#post-406340

Relevant Documentation:

This support ticket is created 7 years, 10 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)

Author
Posts
#406229
back-end post screenshot.png
front-end form screenshot.png

I received excellent support from Minesh regarding autopopulating a CRED form 2nd CPT parent selector with the only the post of the currently logged in user.

This is for a member's site, so user's couldn't assign their 'review' CPTs to other users 'members' CPTs.

These support threads worked so that on the front end everything was fine:
https://toolset.com/forums/topic/populate-cred-form-with-2-parents/#post-404588
https://toolset.com/forums/topic/populate-cred-form-with-2-parents/page/2/#post-405685

So in my form's JS box I have:

/*Only show posts from current user*/
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();
     }
  });

 /*Prepopulate selector with current user post*/
  $('select[name=_wpcf_belongs_member_id] option:eq(1)').attr('selected', 'selected');
  $('select[name=_wpcf_belongs_member_id]').attr('disabled', true);
       
});

And in my functions.php I have:

/*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');

And then I registered the following shortcode in my Views compatibility:

get-parents

As you can see on the front end form screenshot attached, everything looks great. The book review selector and Member selectors are prepopulated, and I can't select any other member.

However when I go to the back end to review the post before publishing, the Member parent hasn't been assigned, you can see on back end post screenshot of the post relationships.

It seems my CRED form is no longer writing the '_wpcf_belongs_member_id' value to the database.

I hope you can help me fix this, it's so close.

Jeff

#406340

I took a step backwards, and removed the last addition to the javascript:

$('select[name=_wpcf_belongs_member_id] option:eq(1)').attr('selected', 'selected');
  $('select[name=_wpcf_belongs_member_id]').attr('disabled', true);

It seems that while this prepopulates the selector, it also stops the post being assigned to its 'Member' parent.

#406466

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Hi Jeff,

So, is that OK if drop-down is enabled rather than disabled?

#406474

Hi Minesh,

I am okay if the dropdown is enabled. Sorry for being so silly, your question pointed me in the right direction. lol

So I readded:

$('select[name=_wpcf_belongs_member_id] option:eq(1)').attr('selected', 'selected');

and made the CRED member parent field required, just incase any user mistakenly deselects their user:

[cred_field field='_wpcf_belongs_member_id' value='' order='date' ordering='desc' required='true' select_text='--- Select member ---' validate_text='member must be selected']

This will do, I've changed my mind about it being necessary to hide it. I may do that later.

Thanks again Minesh that's great. I will also post this to the other thread for others to see.

Jeff

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