外部のWordpressから読み込んだ記事を別のWordpressに自動で投稿したいです
- 評価
- クリップ 1
- VIEW 1,832
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' );
-
気になる質問をクリップする
クリップした質問は、後からいつでもマイページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
クリップを取り消します
-
良い質問の評価を上げる
以下のような質問は評価を上げましょう
- 質問内容が明確
- 自分も答えを知りたい
- 質問者以外のユーザにも役立つ
評価が高い質問は、TOPページの「注目」タブのフィードに表示されやすくなります。
質問の評価を上げたことを取り消します
-
評価を下げられる数の上限に達しました
評価を下げることができません
- 1日5回まで評価を下げられます
- 1日に1ユーザに対して2回まで評価を下げられます
質問の評価を下げる
teratailでは下記のような質問を「具体的に困っていることがない質問」、「サイトポリシーに違反する質問」と定義し、推奨していません。
- プログラミングに関係のない質問
- やってほしいことだけを記載した丸投げの質問
- 問題・課題が含まれていない質問
- 意図的に内容が抹消された質問
- 広告と受け取られるような投稿
評価が下がると、TOPページの「アクティブ」「注目」タブのフィードに表示されにくくなります。
質問の評価を下げたことを取り消します
この機能は開放されていません
評価を下げる条件を満たしてません
質問の評価を下げる機能の利用条件
この機能を利用するためには、以下の事項を行う必要があります。
- 質問回答など一定の行動
-
メールアドレスの認証
メールアドレスの認証
-
質問評価に関するヘルプページの閲覧
質問評価に関するヘルプページの閲覧
check解決した方法
+2
自己解決しました。
完成したソースはこちらです
function profile_info() {
// set the DB creds
$another_db_user = 'user';
$another_db_pass = 'pass';
$another_db_name = 'name';
$another_db_host = 'localhost';
$another_tb_prefix = 'wp_';
$another_post_type = 'your_post_type';
$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 wp_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' => 'your_template.php',
'post_parent' => 'post_parent_ID',
'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_name' => $r->post_name,
'post_excerpt' => $r->post_excerpt,
'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
}
}
}
// using on init to make sure the DB is ready to go
add_action( 'init', 'profile_info' );
投稿
-
回答の評価を上げる
以下のような回答は評価を上げましょう
- 正しい回答
- わかりやすい回答
- ためになる回答
評価が高い回答ほどページの上位に表示されます。
-
回答の評価を下げる
下記のような回答は推奨されていません。
- 間違っている回答
- 質問の回答になっていない投稿
- スパムや攻撃的な表現を用いた投稿
評価を下げる際はその理由を明確に伝え、適切な回答に修正してもらいましょう。
+2
発想は面白いですね。
でもこれだと、すべての記事が再投稿されますよね。
foreachのすぐ下に、投稿する側の記事を調べて、投稿されているかどうか判断し、投稿されていたらスキップするような処理を入れてはどうでしょうか。
重複投稿の判断が肝になりそうですが、Wordpressはあんまり詳しくないんで、調べてません。
頑張ってみてください。
投稿
-
回答の評価を上げる
以下のような回答は評価を上げましょう
- 正しい回答
- わかりやすい回答
- ためになる回答
評価が高い回答ほどページの上位に表示されます。
-
回答の評価を下げる
下記のような回答は推奨されていません。
- 間違っている回答
- 質問の回答になっていない投稿
- スパムや攻撃的な表現を用いた投稿
評価を下げる際はその理由を明確に伝え、適切な回答に修正してもらいましょう。
+1
回答依頼をいただき、ありがとうございます。
どのような目的でそのようなことを実現したいのか意図がつかめないのですが……
もしかしたら、おなじコンテンツを保持したサイトをふたつ作りたいということでしょうか。
そうだとすると、SEOの観点からすると重複コンテンツとみなされ、サイト評価をさげられたり被リンク評価が分散するなど、さまざまな不利益が生じるおそれがありますので注意が必要です。
仮にそうしたことを実現しようとした場合、RSSImport(リンク)というプラグインが役に立つのではないでしょうか。
RSSImport は、他のサイトの最新情報を RSSを使って表示させるプラグインです。くわしい使い方は「RSSImport 使い方」などで検索していただければおわかりになるはずです。
的確な回答になっていないかもしれませんが、以上、ご参考いただければ幸いです。
投稿
-
回答の評価を上げる
以下のような回答は評価を上げましょう
- 正しい回答
- わかりやすい回答
- ためになる回答
評価が高い回答ほどページの上位に表示されます。
-
回答の評価を下げる
下記のような回答は推奨されていません。
- 間違っている回答
- 質問の回答になっていない投稿
- スパムや攻撃的な表現を用いた投稿
評価を下げる際はその理由を明確に伝え、適切な回答に修正してもらいましょう。
+1
依頼ありがとうございます。
瞬時にBサイトも更新というのはなかなか難しいですが、
私ならBでクーロン走らせて
AのAPI作るorREST API使う ですね。
別件ですが、カスタムフィールド値を保存する際は
https://github.com/wokamoto/wp_post_helper
こちらのヘルパーが便利ですよ
投稿
-
回答の評価を上げる
以下のような回答は評価を上げましょう
- 正しい回答
- わかりやすい回答
- ためになる回答
評価が高い回答ほどページの上位に表示されます。
-
回答の評価を下げる
下記のような回答は推奨されていません。
- 間違っている回答
- 質問の回答になっていない投稿
- スパムや攻撃的な表現を用いた投稿
評価を下げる際はその理由を明確に伝え、適切な回答に修正してもらいましょう。
15分調べてもわからないことは、teratailで質問しよう!
- ただいまの回答率 89.97%
- 質問をまとめることで、思考を整理して素早く解決
- テンプレート機能で、簡単に質問をまとめられる
質問への追記・修正、ベストアンサー選択の依頼
2016/03/17 16:51
こちらの質問が他のユーザから「問題・課題が含まれていない質問」という指摘を受けました
teratailでは、漠然とした興味から票を募るような質問や、意見の主張をすることを目的とした投稿は推奨していません。
「編集」ボタンから編集を行い、質問の意図や解決したい課題を明確に記述していただくと回答が得られやすくなります。