我有一个插件,可以创建一个新的数据库表。激活插件后,是否有办法将我的所有帖子ID插入该表?
现在,我正在使用一个动作挂钩来立即插入我发布的新帖子的id。
function transfer_post_id($post_ID) {
global $wpdb;
if (!($wpdb->get_row("SELECT post_id FROM $my_table WHERE post_id = $post_ID" ) ) ) { //if post id not already added
$wpdb->insert( $my_table, array( \'post_id\' => $post_ID ) );
}
return $post_ID;
}
add_action ( \'publish_post\', \'transfer_post_id\' );
然而,在激活插件之前,我需要一种无需手动更新每个插件即可插入的方法。
最合适的回答,由SO网友:Denis de Bernardy 整理而成
Like so:
INSERT INTO $mytable (post_id)
SELECT ID
FROM $wpdb->posts as posts
LEFT JOIN $mytable as dup_check
ON dup_check.post_id = posts.ID
WHERE dup_check.post_id IS NULL;