Alter WordPress sql tables: add a new column
This code can be used during plugin activation. It first gets the results if that mysql column exists already. If true we add a new column to wp_posts with our ALTER TABLE Query and add an Integer field named “my_custom_number”.
Don`t forget to remove the column when your plugin is deleted.
/** * Snippet Name: Alter WordPress sql tables: add a new column * Snippet URL: https://wpcustoms.net/snippets/alter-wordpress-sql-tables-add-new-column/ */ $row = $wpdb->get_results( "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = 'wp_posts' AND column_name = 'my_custom_number'" ); if(empty($row)){ $wpdb->query("ALTER TABLE wp_posts ADD my_custom_number INT(11) NOT NULL DEFAULT 1"); }