WordPress.org

Make WordPress Core

Opened 3 years ago

Closed 2 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)

wordpress-importer.php.diff (1.3 KB) - added by westonruter 3 years ago.
wordpress-importer.php.2.diff (1.3 KB) - added by westonruter 3 years ago.

Download all attachments as: .zip

Change History (14)

#1 @westonruter
3 years ago

In wordpress-importer.php.diff, a new wp_import_existing_post filter is introduced which allows plugins to override what post that post_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 to processed_posts for other posts to refer to during subsequent imports.

#2 @westonruter
3 years ago

  • Milestone changed from Awaiting Review to WordPress.org

#3 @swissspidy
3 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 @westonruter
3 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 @danlouw
3 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!

Last edited 3 years ago by danlouw (previous) (diff)

#6 @westonruter
3 years ago

@Otto42 Can you commit the patch?

#7 @westonruter
3 years ago

  • Owner set to Otto42
  • Status changed from new to reviewing

#8 @spencermorin
3 years ago

+1. This is a welcome improvement.

#9 @Otto42
3 years ago

Sorry, just noticed this. I'll look at getting this into the importer soon.

#11 @dionbeetson
2 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 @Otto42
2 years ago

  • Resolution set to fixed
  • Status changed from reviewing to closed
Note: See TracTickets for help on using tickets.