Opened 9 years ago
Closed 9 years ago
#33721 closed enhancement (fixed)
Add filter to customize result of post_exists()
Reported by: | westonruter | Owned by: | Otto42 |
---|---|---|---|
Milestone: | WordPress.org | Priority: | normal |
Severity: | normal | Version: | |
Component: | Import | Keywords: | has-patch |
Focuses: | Cc: |
Description
The logic in post_exists()
was modified at some point to add a post_date param to help with disambiguating two posts that have the same title (see #6265). Unfortunately, if two posts have been added at the same time and they have the same title then only one will end up getting imported given the current logic in the WordPress importer. I am facing this problem currently.
Some hooks were added in #21710 to make the importer more extensible, but I think another is needed: wp_import_existing_post
This will allow the logic for mapping an importing post to an existing post to be customized, instead of just looking for title and post_date matches. For instance, a GUID could be checked instead (see comment on #21051), though this is dependent on #18315.
Attachments (2)
Change History (14)
#3
@
9 years ago
Just noticed that there's a missing @param
tag for $post
in the filter docblock while having a quick look. Other than that it looks good to me!
#4
@
9 years ago
@swissspidy Good catch! Patch amended.
With this change, a plugin can do the (right thing) and use the GUID as the determination if the post actually already exists, using a plugin like this:
<?php add_filter( 'wp_import_existing_post', function( $post_exists, $post ) { if ( $post_exists ) { $existing_post = get_post( $post_exists ); if ( $existing_post->guid !== $post['guid'] ) { $post_exists = 0; } } return $post_exists; }, 10, 2 );
#5
@
9 years ago
@westonruter I tried your patch and mini-plugin (as a mu-plugin) and can confirm that it resolved import issues for me. Posts that were previously skipped as "duplicates" were successfully imported. Thanks!
#11
@
9 years ago
@Otto42 any update on getting this patch merged in? It would be very useful for our team to have it in master branch. Thanks. Dion
#12
@
9 years ago
- Resolution set to fixed
- Status changed from reviewing to closed
Patch applied to 0.6.2:
https://plugins.trac.wordpress.org/changeset/1409742/wordpress-importer
In wordpress-importer.php.diff, a new
wp_import_existing_post
filter is introduced which allows plugins to override what post thatpost_exists()
detects as being already imported for a given post. This also allows plugins to force a post to be imported. Additionally, it makes sure that the post ID mapping for such existing posts gets added toprocessed_posts
for other posts to refer to during subsequent imports.