如何使用wp_sert_post创建新帖子,但它会无限重复该帖子

时间:2016-03-18 作者:Byron Kevin Hasegawa

this is my source

    global $page;
    $page_exists = $page->post_title;

    $another_db_user = \'user\';
    $another_db_pass = \'pass\';
    $another_db_name = \'name\';
    $another_db_host = \'localhost\';
    $another_tb_prefix = \'wp_\';

    $mydb = new wpdb($another_db_user,$another_db_pass,$another_db_name,$another_db_host);

    $mydb->set_prefix($another_tb_prefix);

    $result = $mydb->get_results("
    SELECT  
    m0.post_title,  
    m0.id,  
    m0.post_name,
    m0.guid  
    FROM ( select post_title, id, guid, post_name from wp_posts where post_type = \'bio\' AND post_status = \'publish\'  ) AS m0 
    ");


    foreach ($result as $value) {


    if( $value->post_title == $page_exists  ) {

    }

    else {
    $my_access = Array(
    \'post_author\' => \'1\',
    \'post_title\' => \'\'.$value->post_title.\'\',
    \'post_status\' => \'publish\',
    \'comment_status\' => \'closed\',
    \'ping_status\' => \'closed\',
    \'post_name\' => \'\'.$value->post_name.\'\',
    \'post_type\' => \'page\',
    \'post_parent\' => \'115822\',
    \'page_template\' => \'template-profile-info.php\'
              );

    wp_insert_post($my_access);

    }

    }
2 个回复
最合适的回答,由SO网友:jgraup 整理而成

对于测试,我只是将外部DB设置为当前DB。正如您在自己的设置中所看到的,它们都应该无法通过要创建的检查,因为它们都存在(B -> B).

显然在你的(A -> B ) 设置只要slug不存在,您将获得许多将通过的设置。

我选择了post_name 结束post_title 因为post_name 必须是唯一的。你也可以对照guid.

function wpse_20160318_do_db_sync() {

    // set the DB creds
    $another_db_user   = DB_USER;
    $another_db_pass   = DB_PASSWORD;
    $another_db_name   = DB_NAME;
    $another_db_host   = DB_HOST;
    $another_tb_prefix = \'wp_\';
    $post_type         = \'post\';

    // setup the DB connection
    $mydb = new wpdb( $another_db_user, $another_db_pass, $another_db_name, $another_db_host );

    $mydb->set_prefix( $another_tb_prefix );

    // query the DB
    $result = $mydb->get_results(
        "
    SELECT ID, post_title, post_name, guid
    FROM $mydb->posts
    WHERE post_status = \'publish\'
        AND post_type = \'$post_type\'
    "
    );

    // for all the results, let\'s check against the slug.
    foreach ( $result as $r ) {

        // see if we can find it...
        $post = get_page_by_path( $r->post_name, OBJECT, $post_type );

        if ( ! $post ) {
            // We\'re cool, let\'s create this one.
            echo PHP_EOL . \'Let\\\'s make a new post for \' . $r->post_title . PHP_EOL;
        }
        else {
            // No need to duplicate this one
            echo PHP_EOL . \'Post already exists for \' . $post->post_title . PHP_EOL;
        }
    }
}

// using on init to make sure the DB is ready to go
add_action( \'init\', \'wpse_20160318_do_db_sync\' );
数据流如下所示:

Query

$result = $mydb->get_results(
    "
    SELECT {PROPERTY}
    FROM $mydb->posts
    "
);

Loop

foreach ( $result as $r ) {

Create

$data = Array(
    \'{PROPERTY}\' => $r->{PROPERTY},
    \'post_name\'  => $r->post_name,
);

wp_insert_post($data);
如果您注释掉测试块,您可以在自己的db上使用该版本。设置$another_post_type 和CPT,它将生成page 对于每个$post_type. 元键还将跟踪ID 从DB使用。由于搜索未指定发布类型,如果垃圾箱中存在重复页面,则在清空垃圾箱之前不会创建新页面。

function wpse_20160318_do_db_sync() {

    $another_db_user = \'user\';
    $another_db_pass = \'pass\';
    $another_db_name = \'name\';
    $another_db_host = \'localhost\';

// FOR TESTING SAME DB
//
//  $another_db_user = DB_USER;
//  $another_db_pass = DB_PASSWORD;
//  $another_db_name = DB_NAME;
//  $another_db_host = DB_HOST;

    $another_tb_prefix   = \'wp_\';
    $another_post_type   = \'bio\';
    $another_post_status = \'publish\';

    $mydb = new wpdb( $another_db_user, $another_db_pass, $another_db_name, $another_db_host );
    $mydb->set_prefix( $another_tb_prefix );
    $result = $mydb->get_results(
        "
    SELECT ID, post_title, post_name, guid, post_content, post_excerpt, post_date, post_date_gmt, post_modified, post_modified_gmt
    FROM $mydb->posts
    WHERE post_status = \'$another_post_status\'
        AND post_type = \'$another_post_type\'
    "
    );

    $another_db_post_id_meta_key = \'another_db_post_id\';

    $post_type = \'page\';
    $mods      = array (
        \'post_status\'    => \'publish\',
        \'post_author\'    => \'1\',
        \'comment_status\' => \'closed\',
        \'ping_status\'    => \'closed\',
        \'page_template\'  => \'template-profile-info.php\',
        \'post_parent\'    => \'115822\',
        \'post_type\'      => $post_type,
    );

    foreach ( $result as $r ) {

        $post_title = wp_strip_all_tags( $r->post_title );

        // see if we can find it by title...
        $post = get_page_by_title( $post_title, OBJECT, $post_type );

        if ( ! $post ) {

            // title wasn\'t found... but let\'s check the meta
            $meta_args  = array (
                \'post_type\'  => array ( $post_type ),
                \'meta_query\' => array (
                    array (
                        \'key\'     => $another_db_post_id_meta_key,
                        \'compare\' => \'=\',
                        \'type\'    => \'NUMERIC\',
                        \'value\'   => $r->ID,
                    ),
                ),
            );
            $meta_posts = get_posts( $meta_args );

            // set the post to the first item in the list, if it isn\'t empty
            if ( ! empty( $meta_posts ) ) {
                list( $post ) = $meta_posts;
            }
        }

        if ( ! $post ) {

            // Can\'t find the post, let\'s create this one.

            // Data from another DB
            $clone = array (
                \'post_title\'        => $post_title,
                \'post_content\'      => $r->post_content,
                \'post_excerpt\'      => $r->post_excerpt,
                \'post_date\'         => $r->post_date,
                \'post_date_gmt\'     => $r->post_date_gmt,
                \'post_modified\'     => $r->post_modified,
                \'post_modified_gmt\' => $r->post_modified_gmt,
            );

            // Merge with our defaults
            $args = array_merge( $clone, $mods );

            // create new post
            $post_id = wp_insert_post( $args );

            if ( $post_id instanceof WP_Error ) {
                // failed to create new post
                // wp_die( \'Failed to create \' . $post_id );
            }
            else {
                // new post created
                $post = get_post( $post_id );

                // add clone tag that we can check later
                add_post_meta( $post->ID, $another_db_post_id_meta_key, $r->ID, false );
            }
        }
        else {
            // no need to dup
        }
    }
}

SO网友:Chittaranjan

我直接指的是表格wp_posts 您可以设置并使用前缀,而不是使用任何前缀,因为您肯定知道表名。

// Get all posts in the current database
$this_posts = get_posts(
                array(
                    \'post_type\' => \'page\',
                    \'numberposts\' => -1
                )
            );

// Pull all titles for duplication checking
$this_post_titles = wp_list_pluck( $this_posts, \'post_title\' );

// Set other database credentials and connect
$another_db_user = \'user\';
$another_db_pass = \'pass\';
$another_db_name = \'name\';
$another_db_host = \'host\';
$another_tb_prefix = \'wp_\';

$mydb = new wpdb( $another_db_user, $another_db_pass, $another_db_name, $another_db_host );

// Get all data
$result = $mydb->get_results(
                        "SELECT post_title, post_name FROM wp_posts 
                        WHERE post_type = \'bio\' AND post_status = \'publish\'"
                    );

// Loop through each result and insert if it\'s not there in the system    
foreach ( $result as $value ) {
    if ( in_array( $value->post_title, $this_post_titles ) ) {
        echo \'Already exisis: \' . $value->post_title;
    } else {
        $my_access = array(
                    \'post_author\' => \'1\',
                    \'post_title\' => $value->post_title,
                    \'post_status\' => \'publish\',
                    \'comment_status\' => \'closed\',
                    \'ping_status\' => \'closed\',
                    \'post_name\' => $value->post_name,
                    \'post_type\' => \'page\',
                    \'post_parent\' => \'115822\',
                    \'page_template\' => \'template-profile-info.php\'
                  );

        $inserted = wp_insert_post( $my_access );

        if ( $inserted )
        {
            echo \'Added: \' . $value->post_title;
        }
        else
        {
            echo \'Failed: \' . $value->post_title;
        }
    }

    echo \'<br />\';
}