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