• Resolved techboomie

    (@techboomie)


    hi,

    is there a hack to automatically overwrite existing post or permalink if the title is the same?

    For example, i made a post with title “wordpress rules” and it made a permalink of
    example.com/wordpress-rules.htm

    then on another day, i thought i should write a new post (not edit the old post) and used the title “wordpress rules” again.
    i want the output permalinks to be
    example.com/wordpress-rules.htm
    and not
    example.com/wordpress-rules-2.htm
    as wordpress would automatically add an increment number to the permalinks

Viewing 9 replies - 1 through 9 (of 9 total)
  • Joey

    (@leglesslizard)

    Hey, chucked together a one function plugin to try and achieve what you wanted:

    <?php
    /*
    Plugin Name: Overwrite WordPress Posts if Title Already Used
    Description: Checks to see if post title has already been used and if so deletes the old post and creates a new one with the given data.
    Author: JPG
    Version: 1.0
    */
    
    // Filter used to access data before it is written to the database
    add_filter( 'wp_insert_post_data', 'overwrite_wordpress_post_if_title_exists', 99, 2 );
    
    // Our function to delete the old post if there is a title clash (title must be identical)
    function overwrite_wordpress_post_if_title_exists( $data, $postarr ) {
    
    	// Return if we aren't saving/updating a post
    	if ( $data['post_status'] !== 'publish' ) {
    		return $data;
    	}
    
    	// Return all existing posts
    	$posts = get_posts( array( 'post_type' => 'post' ) );
    
    	// Loop through exisiting posts
    	foreach ( $posts as $post ) {
    
    		// If title already exists, delete the old post before we save the new one (change 'false' to 'true' to delete the post rather than trash it)
    		if ( get_the_title( $post ) == $data['post_title'] && $post->ID != $postarr['ID'] ) {
    			wp_delete_post( $post->ID, false );
    			$data['post_name'] = sanitize_title( $data['post_title'] );
    		}
    	}
    	return $data;
    }
    ?>

    Not tested excessively but figured it gives you a starting point if this looks like it is close to what you wanted. Be aware it currently makes adjustments when you are publishing a post (if status is anything other it won’t do anything).
    Also, as the note says with wp_delete_post set to false exisiting posts will get trashed NOT deleted permanently (figured this was safer in case you want to recover an old post).
    This does, however, mess with your permalinks a tad in that in the post edit screen you will see the ‘wordpress-rules-2’ as before but when you are on the frontend and viewing the post it should use the desired url: ‘wordpress-rules’.

    Be sure to test it fully with dummy data before you go deleting anything important ??
    Hope this helps a tad!

    Thread Starter techboomie

    (@techboomie)

    thanks a lot but it doesn’t seem to work ..

    Joey

    (@leglesslizard)

    Working on my install so sorry I’m not sure what to suggest. This is only currently looking at posts (post_type = post) so won’t see any custom post types or pages unless you change that. Also, will only affect posts that are being published, so any other status wont be affected unless you modify it.
    All I can suggest is try deactivating other plugins to see if anything clashes with it and then can look again for you if none of that solves the issue.

    I have it working with no other plugins activated on wordpress 4.0.1

    Thread Starter techboomie

    (@techboomie)

    Thanks again. I tried the plugin in on a new install. This plugin works great..However the title has to match in order for it to overwrite and is case sensitive. Let’s say i do a post name HELLO or HELLO! and do another second post name hello, it will create a new post instead of overwriting it. Is there a way to ignore the case sensitive issue or special character issue.

    Joey

    (@leglesslizard)

    Have added a function to clean the strings and change to lower case before the comparison. It still saves the new post as whatever the user enters however. For example if I create a post titles ‘Hello There!’, then another titled ‘hello!There!$%£$’ a new post titled ‘hello!There!$%£$’ will be created and the original moved to trash.

    Obviously you can juggle the code around if this isn’t what you wanted and google will have the answer for any more preg_replace expressions you may need to add ??

    <?php
    /*
    Plugin Name: Overwrite WordPress Posts if Title Already Used
    Description: Checks to see if post title has already been used and if so deletes the old post and creates a new one with the given data.
    Author: JPG
    Version: 1.0
    */
    
    // Filter used to access data before it is written to the database
    add_filter( 'wp_insert_post_data', 'overwrite_wordpress_post_if_title_exists', 99, 2 );
    
    // Our function to delete the old post if there is a title clash (title must be identical)
    function overwrite_wordpress_post_if_title_exists( $data, $postarr ) {
    
    	// Return if we aren't saving/updating a post
    	if ( $data['post_status'] !== 'publish' ) {
    		return $data;
    	}
    
    	$new_title = my_clean_string_function( $data['post_title'] );
    	$new_title = strtolower( $new_title );
    
    	// Return all existing posts
    	$posts = get_posts( array( 'post_type' => 'post' ) );
    
    	// Loop through exisiting posts
    	foreach ( $posts as $post ) {
    
    		$old_title = my_clean_string_function( get_the_title( $post ) );
    		$old_title = strtolower( $old_title );
    
    		// If title already exists, delete the old post before we save the new one (change 'false' to 'true' to delete the post rather than trash it)
    		if ( $old_title == $new_title && $post->ID != $postarr['ID'] ) {
    			wp_delete_post( $post->ID, false );
    			$data['post_name'] = sanitize_title( $data['post_title'] );
    		}
    	}
    	return $data;
    }
    
    function my_clean_string_function( $string ) {
    
       return preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars.
    }
    ?>

    All the best

    Thread Starter techboomie

    (@techboomie)

    omg you are the best. this is exactly what i wanted. THank you Thank you Thank You.

    Joey

    (@leglesslizard)

    Very welcome. Can you mark the issue as resolved please ??

    All the best

    Thread Starter techboomie

    (@techboomie)

    Thanks

    Wow, this function is awesome, thanks for sharing, would it be possible to make wordpres do nothing if the post already exist?

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Is there a way to overwrite existing post?’ is closed to new replies.