How do I tie a post type to a user, so they can create only 1 post, and it is correctly tied to their account (so they are the only person that can edit it, aside from administrators). They will each have a members dashboard screen, so I plan to have a tab there for the directory listing, and it should show either a button to create one, or edit the current one if they have one.
Hi, there are some techniques for relating Users and posts described in this document:
https://toolset.com/documentation/user-guides/how-to-create-custom-searches-and-relationships-for-users/#how-do-i-prevent-users-from-creating-more-than-one-contractor-post
The general idea is the User will be the author of the post they create. This means you can create a View of those member posts, filtered by post author, where the post author is the same as the current logged-in User. If any results are found, you know that the User has already created a member post. So in the "wpv-items-found" section of the View, you will display information about that member post using shortcodes. In the "wpv-no-items-found" section, you will place the new member post Form, so the User can create their one and only member post. Then insert this View on your custom profile page.
Excellent Christian. The only concern I can think about this is that it is possible that they could submit more than one if they opened a few windows with no posts, and submitted them. Any suggestions how I can do a quick verification on submit to ensure they don't have another post already?
Thanks Christian!
-Mark
Preventing multiple submissions programmatically will require some custom code using our Forms API. We offer the API hook cred_form_validate to allow you to perform any custom validation. Another ticket in the forum includes a similar question: https://toolset.com/forums/topic/cred-before-save-data-allow-only-one-submition-of-the-form/
Here's an edited version of the validation code:
add_filter('cred_form_validate','profile_exists_validation',10,2);
function profile_exists_validation($error_fields, $form_data)
{
global $current_user;
//field data are field values and errors
list($fields,$errors)=$error_fields;
//validate if specific form
if ($form_data['id']==12345)
{
//check if current User is already the author of a "Profile" custom post
$args = array(
'author' => $current_user->ID,
'orderby' => 'post_date',
'order' => 'ASC',
'posts_per_page' => 1,
'post_type' => 'profile'
);
$profiles = get_posts( $args );
if ( sizeof($profiles) > 0 )
{
//set error message for post title
$errors['post_title'] = 'Profile already exists, so you cannot create another one.';
}
}
//return result
return array($fields,$errors);
}
Replace 12345 with the numeric ID of this Form. Replace profile in the $args array with the proper post type slug, and modify the message however you would like. Then you can add this code to your child theme's functions.php file, or you can add it to the new code snippets section in Toolset > Settings > Custom Code.