I need to be able to merge two or more posts. What is the exact query (or queries) to make this happen?
The scenario is multiple recommendations that need to be merged into one. I have already sorted out comments merging and meta merging.
Let’s say a user recommends Bill Gates, another one Gates Bill and another one makes a typo and adds Bil Gates. I need to merge these three posts and choose the destination one.
UPDATE:
What I need is a complete SQL query for deleting a post and all references to that post. Something like this pseudo-code:
DELETE FROM wp_posts WHERE id = X;
DELETE FROM wp_posts_meta WHERE id = X;
DELETE FROM wp_another_table WHERE id = X;
DELETE FROM wp_yet_another_table WHERE id = X;
where X is the ID of my post.
The code (or plugin) shouldn’t auto-detect duplicates. I will present the user a dropdown with a list of posts and the “Delete” option. Before deleting, I will copy the post content from one post to the other using an SQL UPDATE query.
UPDATE 2:
I think I found something, I’ll have to check though:
$custom_field_keys = get_post_custom_keys($postid);
foreach($custom_field_keys as $key => $value) {
$valuet = trim($value);
if('_' != $valuet{0} ){
delete_post_meta($postid, $key, '');
}
}
$result = wp_delete_post($postid);
The requirements you are describing require much more of an explanation than is really suitable here. A simple solution might be to mark the duplicate posts as duplicates via a meta box on the post screen. This meta box would have one input: A reference to another post.
Check out this tutorial on how to build a custom meta box.
Suppose you’ve set up a meta box that will allow the user to mark a post that is a duplicate of another post. Say the duplicate post is marked with a meta key
_duplicate_of_post
, whose meta value is the canonical post’s post ID.You can then on filter
the_posts
filter to pull together any duplicates, folding the the canonical post together with any of its duplicates:One caveat in implementation of the post reference meta box is that you’d have to implement the logic to ensure that you can’t set a post as a duplicate post is if it’s already marked as a “master” post by another post. Without that restriction, the example above will break in unexpected ways.
Update
You can also hook on
save_post
, combine, then trash posts marked as duplicates:This is the most basic of examples. It’s expecting a form field on the edit post screen named
_duplicate_of_post
which contains the canonical post’s ID. Note that I’m using ONLY the WordPress API, which is something you should always aspire to do while developing theme and plugin code.