I have a plugin which creates a new database table. Upon activating the plugin is there a way to insert all my posts ids into that table?
Right now I’m using an action hook to immediately insert the id of new posts I publish.
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' );
However there are posts before activating the plugin that I need a way to insert without manually updating each one.
Like so:
You can design your table in such a way that an ID can only exists once by using a key. You can then run a insert into query that selects existing ID values directly with a subselect.
Like so (credits Denis):
Check the MySql manual regarding the syntax of the INSERT statement.
I’m so sorry! I lost login please forgive me, apparently I used an incorrect email. I’ll contact the forum admin to see how I can recover my username..
Denis thanks for the code!
I’m using it like so
I deactivated the plugin and upon activation to test, I get
I have wp_debug on but I’m not getting any detailed errors.