How to display data from custom table in wordpress database?

Here is an example code that will get the data and then display it:

    global $wpdb;
    // this adds the prefix which is set by the user upon instillation of wordpress
    $table_name = $wpdb->prefix . "your_table_name";
    // this will get the data from your table
    $retrieve_data = $wpdb->get_results( "SELECT * FROM $table_name" );
?>
<ul>
    foreach ($retrieve_data as $retrieved_data){ ?>
        <li><?php echo $retrieved_data->column_name;?></li>
        <li><?php echo $retrieved_data->another_column_name;?></li>
        <li><?php echo $retrieved_data->as_many_columns_as_you_have;?></li>
    <?php 
        }
    ?>
</ul>
<?php

It’s good practice to use unique names for variables and functions, so you may want to add a unique prefix to all your variables or functions IE: ($prefix_table_name where “prefix” would be something unique such as the abbreviation of your theme or plugin.)

Reference – wpdb – codex

Leave a Comment