Get Database Table Column Names In WordPress.



Some times we need to get the column names for a MySQL database table in WordPress,Here in this post i am describing how to do this. It is very simple, suppose you want to get the column name for the post code is given below:
<?php

global $wpdb;
$table_name = $wpdb->prefix . 'posts';
foreach ( $wpdb->get_col( "DESC " . $table_name, 0 ) as $column_name ) {
  error_log( $column_name );
}
?>

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

ID
post_author
post_date
post_date_gmt
post_content

You have done, Enjoy the WordPress coding.