My code for ALTER TABLE
doesn't make use of the prepare()
method of $wpdb
and so the code needs to be updated properly. There are plenty of examples of query SELECT
, etc but couldn't find something for ALTER TABLE
with several options included in the SQL.
This is the old code.
$post_qry = $wpdb->get_row( "SELECT * FROM $wpdb->posts" );
if ( ! isset( $post_qry->forum_id ) ) {
$wpdb->query( "ALTER TABLE {$wpdb->posts} ADD `forum_id` INT( 10 )
UNSIGNED NOT NULL DEFAULT '0' AFTER `ID` " );
}
This is my first attempt, but something doesn't look right for the ADD.
if ( ! isset( $post_qry->forum_id ) ) {
$wpdb->query( $wpdb->prepare( "ALTER TABLE {$wpdb->posts} ADD col= %s INT( 10 )
UNSIGNED NOT NULL DEFAULT '0' AFTER col = %s ", 'forum_id', 'ID' ) );
}
First, is it proper to use
ADD col =
or is itADD COLUMN=%s
?Second, is the same true for stating after
col=%s
?Third, should
INT
andUNSIGNED
be used as%s
also?
Thank you for your time to explain and maybe point to a good resource.
forum_id
is a bad idea, you should use post meta to store values, or use a custom taxonomy if you're trying to organise postsWP_Query
loops and calls toget_post_meta
, eliminate your table creation and update code entirely, and as a bonus you get a free GUIupdate_post_meta ( $post->ID , 'forum_id', $thread_id );
instead of all the mysql code. Retrieving the meta data was just as easy.