WordPress.org

Support

Support » How-To and Troubleshooting » How to get a custom table in the wordpress database

How to get a custom table in the wordpress database

  • I’ve placed the following function in my functions.php file. I am trying to get data out of a table I created in the wordpress database. It’s giving me this error when I try to call it: Fatal error: Call to a member function get_results() on a non-object in C:\Program Files\wamp\www\wordpress\wp-content\themes\Theme 1\functions.php on line 8

    Line 8 is the $themes = …

    function theme_query () {    
    
    	$querystr = "SELECT * FROM custom_themes";
    	$themes = $wpdb->get_results($querystr, OBJECT);
    
    foreach ($themes as $themes) {
    	echo $themes->title;
    }
    }

    What am I doing wrong?

Viewing 3 replies - 1 through 3 (of 3 total)
  • You’re missing the “global $wpdb;” declaration at the top of your function.

    Looks like I needed to add a:

    global $wpdb;

    So, the code should be:

    function theme_query () {    
    
    global $wpdb;
    
    	$querystr = "SELECT * FROM custom_themes";
    	$themes = $wpdb->get_results($querystr, OBJECT);
    
    foreach ($themes as $themes) {
    	echo $themes->title;
    }
    }

    I swear I didn’t steal it from you pizdin! Thanks for replying though.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to get a custom table in the wordpress database’ is closed to new replies.