[ad_1]
Hello!
Possible make this?
I want to create a new post, and there is already an older one with the same name, overwriting the old one.
Normally, if there is a post with the same name, the new one is automatically renamed.
I want it to be overwritten the older automatic.
[ad_2]
You should do scheduled updates of existing posts. Otherwise you risk deleting active content. The reason this functions the way it does is that no two posts/pages etc can share the same slug/permalink.
You can rename the slug of the old one ( usually add -old to the slug and title) and then swap the slug on the new one to that permalink.
>I want it to be overwritten the older automatic.
I am afraid, but this isn’t possible “out of the box”, as WordPress works exactly in the opposite way – it never allows overwriting of the same titles, categories, etc, but it renames them automatically.
Maybe you can try with the cusomt code/snippets, with a plugin like “Code Snippets”: [https://wordpress.org/plugins/code-snippets/](https://wordpress.org/plugins/code-snippets/).
Add a new snippet with the following code to check for post titles and overwrite if a match is found:
add_action(‘wp_insert_post_data’, function($data, $postarr) {
global $wpdb;
// Only proceed if this is a new post (no ID set)
if (empty($postarr[‘ID’])) {
// Prepare the query to find an existing post with the same title, type, and status
$query = $wpdb->prepare(
“SELECT ID FROM $wpdb->posts WHERE post_title = %s AND post_type = %s AND post_status = %s”,
$data[‘post_title’], $data[‘post_type’], ‘publish’
);
$existing_post_id = $wpdb->get_var($query);
// Check for database errors
if ($wpdb->last_error) {
wp_die(‘Database error: ‘ . $wpdb->last_error);
}
// If an existing published post with the same title and type is found
if ($existing_post_id) {
// Optionally, you could handle redirection or other logic here
// For instance, prevent the new post insertion by redirecting or stopping execution:
// wp_redirect(get_permalink($existing_post_id));
// exit;
// Set the ID to update the existing post instead of creating a new one
$data[‘ID’] = $existing_post_id;
}
}
return $data;
}, 10, 2); // Set a lower priority to allow other plugins to modify $data first
Save and activate this snippet.
This code purpose is to verify if there are any existing posts with the same title before publishing a new post. In case a match is found, the new post data will be linked to the ID of the existing post, essentially replacing the old post.
PS Before making any changes to your site, including deactivating/installing the new plugins, updating existing plugins/theme, testing snippets/code, … you should first backup your site to be on the safe side: [https://www.wpbeginner.com/plugins/7-best-wordpress-backup-plugins-compared-pros-and-cons/](https://www.wpbeginner.com/plugins/7-best-wordpress-backup-plugins-compared-pros-and-cons/) (I use All in one WP migration plugin, and you have other plugins such as Duplicator, UpdraftPlus, WPVivid, etc….).
If I understand you correctly you’re saying you want to completely replace the content of a current post while keeping the name and ‘permalinl’ URL.
A trivial but credible example might be overwriting a post called “Five New Ideas for April.” Where each April you offer five new ideas and don’t want anyone to be confused when they stumble across *last* April’s five new ideas.
If that’s what you’re looking for then it’s fine to just replace the old content and change the publication date. With a few caveats — for instance an SEO advisor at TowerAdvising recommends putting in a disclaimer, e.g.
>Step 5: Make sure to add an editor’s note at the bottom of the updated post to clarify. That way if someone finds your blog but sees comments on it from before the publish date, it doesn’t look suspicious.
> Eg., editor’s note: “This blog was originally published on April 25, 2019. It was updated on March 25, 2022.”
They actually give a complete rundown on how to do this here: https://www.towermarketing.net/blog/updating-old-blog-posts-for-seo/
(Note: I’m not endorsing that website, I’m just linking the first site I found that gave a credible answer. In this case it’s a pretty good answer but most blogging and SEO experts will give you similar advice.)