Skip Navigation

[Resolved] Associate a post with a User and only allow User to create that one post

This support ticket is created 6 years, 2 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 3 replies, has 2 voices.

Last updated by Christian Cox 6 years, 1 month ago.

Assisted by: Christian Cox.

Author
Posts
#1109526

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.

#1109723

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.

#1113819

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

#1113872

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.