LoginSignup
4
4

More than 5 years have passed since last update.

外部のWordpressから読み込んだ記事を別のWordpressに自動で投稿したいです

Posted at

Aサイトから読み込んだ記事をBサイトに自動で投稿させたいです。

また、そのAサイトの記事を更新したらBサイトで自動生成された記事も更新されるようにしたいです。

今の段階でのソースです。
`
global $wpdb;
$results = $wpdb->get_results("
SELECT post_title
FROM wp_posts
");

foreach ($results as $post) {
$page_exists = $post->post_title;
}

//その他DB接続設定
$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);
}

}
`

これだと記事は重複されて繰り返し自動投稿します。

別のソースの案

$this_posts = get_posts(
                array(
                    'post_type' => 'page',
                    'page_template' => 'template-profile-info.php'
                )
            );

$this_post_titles = wp_list_pluck( $this_posts, 'post_title' );

// set the DB creds
$another_db_user = 'user';
$another_db_pass = 'pass';
$another_db_name = 'name';
$another_db_host = 'localhost';
$another_tb_prefix = 'wp_';

// 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 );

//一覧情報取得
$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 ( in_array( $value->post_title, $this_post_titles ) ) {

    } 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 );
    }
}

さらに別のソースの案

function wpse_20160318_do_db_sync() {

    // query the DB
    global $wpdb;
    $result = $wpdb->get_results(
        "
    SELECT post_name
    FROM wp_posts
    WHERE post_status = 'publish'
    AND post_type = 'page'
    "
    );



    // 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 );

    }

    // set the DB creds
    $another_db_user = 'user';
    $another_db_pass = 'pass';
    $another_db_name = 'name';
    $another_db_host = 'localhost';
    $another_tb_prefix = 'wp_';

    // 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 );

    //一覧情報取得
    $results = $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 ( $results as $value ) {


        if ( ! $post ) {
            // We're cool, let's create this one.
            $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); 

        }
        else {
            // No need to duplicate this one

        }

    }


}

// using on init to make sure the DB is ready to go
add_action( 'init', 'wpse_20160318_do_db_sync' );
4
4
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4