How to show data from custom database table in WordPress

Show data from WordPress custom database table using the $wpdb select query to get specific data and show it to the front end.

See the example below, here I have used WordPress function get_results() to select the customer’s whos city is Mumbai.

global $wpdb;
$customers = $wpdb->get_results("SELECT * FROM wp_customer_tbl WHERE city = 'Mumbai'");

<table class="table table-hover">

    <?php foreach($customers as $customer){ ?>
         <td><?php echo $customer->order_id; ?></td>
         <td><?php echo $customer->customer_id; ?></td>
         <td><?php echo $customer->customer_name; ?></td>
         <td><?php echo $customer->phone; ?></td>
         <td><?php echo $customer->address; ?></td>
<?php } ?>


As get_results() returns multiple rows of data we have used foreach loop to display data one by one. If you want to receive only single row data then you should use get_row() function.

To read the complete information about the WPDB class and its functions please visit here.


To learn about WP_Query for custom post type with a custom taxonomy please click here.

Comments are closed, but trackbacks and pingbacks are open.