Wordpress: retrieve values from a custom table in the Wordpress database


/ Published in: PHP
Save to your folder(s)

Refer to the following:
http://codex.wordpress.org/Class_Reference/wpdb


Copy this code and paste it in your HTML
  1. <?php
  2. /*
  3. Say you created a
  4. TABLE named food
  5. in the wordpress database
  6. WITH THE COLUMNS
  7. id
  8. name
  9. organic YES/NO
  10. */
  11.  
  12. global $wpdb;
  13.  
  14. $query = 'SELECT id, name, organic FROM food';
  15.  
  16. $rows = $wpdb -> get_results($query);
  17.  
  18. foreach ($rows as $key=> $value) {
  19. $id = $value->id;
  20. $name = $value->name;
  21. $organic = value->organic;
  22. ?><p><?php echo $id; ?></p><?php
  23. ?><p><?php echo $name; ?></p><?php
  24. ?><p><?php echo $organic; ?></p><?php
  25. }
  26. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.