WordPress Get DB Table Column Names

To get the column names for a MySQL database table in WordPress, use the following:

global $wpdb;

$table_name = $wpdb->prefix . 'posts';

foreach ( $wpdb->get_col( "DESC " . $table_name, 0 ) as $column_name ) {
  error_log( $column_name );
}

Which will result in something like the following being printed to your debug.log:

ID

post_author

post_date

post_date_gmt

post_content

...

You get the idea 🙂