How to import a custom MySQL DB table into a WordPress custom post type

WordPress Posts are not stored in a single table. Postmeta is stored elsewhere, and of course each table has its own format. If you ever publish content directly in WordPress (posts, pages, attachments, etc.) you could quickly run into issues with post IDs. Even if not, it would potentially require some fairly significant data manipulation … Read more

Create table from array with prepare

I tried this in local, and I think you have multiple fields inside $fields so I’ve added them in array. Just look at the code below; it works fine as tested: add_action(‘your_hook’, ‘createTableFromFields’); function createTableFromFields($tablename) { $wpdb = $this->db; $tablename = $wpdb->prefix . $tablename; $fields = array(‘PersonID’,’LastName’); $sql=”CREATE TABLE IF NOT EXISTS %s (id INT(6) … Read more

Fetching values from database for select box

You can used below code to get results from the database in wordpress. global $wpdb, $posts; $prefix = $wpdb->prefix; $query = $wpdb->get_results(“SELECT * FROM {$prefix}states”); if($query){ echo ‘<select><option value=”select”>Select</option>’; foreach ($query as $value) { echo ‘<option value=”‘.$value->ID.'”>’.$value->ID.'</option>’; } echo “</select>”; }