Skip to content

Instantly share code, notes, and snippets.

@romit19
Forked from ssuess/wordpress-import-update.php
Created January 10, 2019 19:25
Show Gist options
  • Select an option

  • Save romit19/d7df064d089922f038d1a1f7c939964d to your computer and use it in GitHub Desktop.

Select an option

Save romit19/d7df064d089922f038d1a1f7c939964d to your computer and use it in GitHub Desktop.

Revisions

  1. @ssuess ssuess revised this gist Sep 1, 2018. 1 changed file with 6 additions and 7 deletions.
    13 changes: 6 additions & 7 deletions wordpress-import-update.php
    Original file line number Diff line number Diff line change
    @@ -23,16 +23,15 @@ function wp_import_existing_post( $post_id, $post ) {
    if ( $this->existing_post = $post_id ) {
    error_log('old post id found: '. $post_id); // log old post id
    if ( get_post_type( $post_id ) == 'attachment' ) { // check if this is attachment
    error_log("but " . $post_id . " is an attachement, so ignoring"); // log attachement ignoring
    $post_id = $post_id; // if attachment ignore, set post_id to real id
    error_log("but " . $post_id . " is an attachement, so ignoring"); // log attachement ignoring
    $post_id = $post_id; // if attachment ignore, set post_id to real id
    } else {
    global $wpdb;
    $wpdb->delete( 'wp_postmeta', array( 'post_id' => $post_id ) ); //delete existing meta (assumes import contains all meta needed for post)
    $post_id = 0; // force the post to be imported
    global $wpdb;
    $wpdb->delete( 'wp_postmeta', array( 'post_id' => $post_id ) ); //delete existing meta (assumes import contains all meta needed for post)
    $post_id = 0; // force the post to be imported
    }

    } else {
    error_log('new post created.'); // log new post created
    error_log('new post created.'); // log new post created
    }
    return $post_id;
    }
  2. @ssuess ssuess revised this gist Sep 1, 2018. 1 changed file with 8 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions wordpress-import-update.php
    Original file line number Diff line number Diff line change
    @@ -21,10 +21,18 @@ function __construct() {

    function wp_import_existing_post( $post_id, $post ) {
    if ( $this->existing_post = $post_id ) {
    error_log('old post id found: '. $post_id); // log old post id
    if ( get_post_type( $post_id ) == 'attachment' ) { // check if this is attachment
    error_log("but " . $post_id . " is an attachement, so ignoring"); // log attachement ignoring
    $post_id = $post_id; // if attachment ignore, set post_id to real id
    } else {
    global $wpdb;
    $wpdb->delete( 'wp_postmeta', array( 'post_id' => $post_id ) ); //delete existing meta (assumes import contains all meta needed for post)
    $post_id = 0; // force the post to be imported
    }

    } else {
    error_log('new post created.'); // log new post created
    }
    return $post_id;
    }
  3. @ssuess ssuess revised this gist Sep 1, 2018. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions wordpress-import-update.php
    Original file line number Diff line number Diff line change
    @@ -21,7 +21,10 @@ function __construct() {

    function wp_import_existing_post( $post_id, $post ) {
    if ( $this->existing_post = $post_id ) {
    global $wpdb;
    $wpdb->delete( 'wp_postmeta', array( 'post_id' => $post_id ) ); //delete existing meta (assumes import contains all meta needed for post)
    $post_id = 0; // force the post to be imported

    }
    return $post_id;
    }
  4. @balbuf balbuf created this gist Oct 15, 2016.
    38 changes: 38 additions & 0 deletions wordpress-import-update.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    <?php

    /**
    * When using the WordPress Importer, update existing
    * posts instead of skipping them. Updates content according
    * to the import file even if the existing post was updated
    * more recently.
    *
    * To use, drop this file into your /mu-plugins/ folder or
    * copy this code into your functions.php file.
    */

    class WPImporterUpdate {

    protected $existing_post;

    function __construct() {
    add_filter( 'wp_import_existing_post', [ $this, 'wp_import_existing_post' ], 10, 2 );
    add_filter( 'wp_import_post_data_processed', [ $this, 'wp_import_post_data_processed' ], 10, 2 );
    }

    function wp_import_existing_post( $post_id, $post ) {
    if ( $this->existing_post = $post_id ) {
    $post_id = 0; // force the post to be imported
    }
    return $post_id;
    }

    function wp_import_post_data_processed( $postdata, $post ) {
    if ( $this->existing_post ) {
    // update the existing post
    $postdata['ID'] = $this->existing_post;
    }
    return $postdata;
    }

    }
    new WPImporterUpdate;