Skip Navigation

[Resolved] Issue with duplicate post titles when editing

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

Problem:
The user would like to allow users to create posts, from the frontend, with unique titles. He uses custom code for validation, but the custom code fails for updates as the new title equals the old(existing) one.

Solution:
Introduce a check in the code that will handle the update case. Check reply https://toolset.com/forums/topic/issue-with-duplicate-post-titles-when-editing/#post-1823283

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/

This support ticket is created 3 years, 6 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 - - 9: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: Africa/Casablanca (GMT+00:00)

This topic contains 4 replies, has 3 voices.

Last updated by aaronW-4 3 years, 5 months ago.

Assisted by: Jamal.

Author
Posts
#1821671

Tell us what you are trying to do?
I am implementing code to prevent duplicate post titles. I used a ticket from 2019 and it works great... The problem is that I have an edit form for posts and it won't save changes because the post title is the same as the title of the post you are editing. I could just remove the code from the edit form, but if the user decides to edit the name of the post, they shouldn't be able to use a name that someone else might already have. So, for the edit form, I need to prevent duplicate post titles but disregard the name of the post they are editing.

Is there any documentation that you are following?
https://toolset.com/forums/topic/prevent-duplicate-post-titles-from-being-created/

Is there a similar example that we can see?

What is the link to your site?
hidden link

#1822199

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Do you want to share the final version of the code you are using? (That thread includes a lot of back and forth about it.)

#1822423

Of course. Here is what I added for the post creation form and the post edit form:

 
/* Validate CRED Forms */
add_filter('cred_form_validate','func_validate_title',10,2);
function func_validate_title($error_fields, $form_data){
     
    list($fields,$errors)=$error_fields;
     
    $forms_array =  array( 20, 23 );
 
    if (in_array($form_data['id'], $forms_array)) {
 
        // get the title from the form
        $title = $_POST['post_title'];
 
        // query to get the posts to compare
        $args = array( "post_type" => "crowd", "posts_per_page" => -1, "post_status" => array('publish', 'draft') );
        $query = get_posts( $args );
 
        if (count($query) > 0){
 
            //set error message for the title field if a post of the same title exists
            foreach ($query as $post) {
                if (strtolower($post->post_title) == strtolower($title)) {
                    $errors['post_title']='Duplicate Title! Add another!';
                    break; 
                }
            }
        }
    }
  
    //return result
    return array($fields,$errors);
}
#1823283

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+00:00)

You will need to pull the current title from the database and compare it to the newly typed title. Put the following code at line 10:

// get post_id
$post_id = $form_data['container_id'];
// get the post
$post = get_post( $post_id );
// If title is the same return, otherwise continue
if ( $post->post_title == $_POST['post_title']) return array($fields,$errors);
#1823289

My issue is resolved now. Thank you!

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