Steve
Steve

Reputation: 4908

Error from WordPress get_results() call

I have a jQuery .load() in a script:

$('#category2').load("/wp-content/themes/mydomain/categories.php",
                            { category_number : 2,   // category to get
                              category_1 : category_1,
                              category_2 : category_2,
                              category_3 : category_3,
                              category_4 : category_4,
                              category_5 : category_5,
                              category_6 : category_6,
                              category_7 : category_7
                            },
                            function(responseTxt, textStatus, xhr) {
                                console.log("responseTxt=" + responseTxt + ", textStatus=" + textStatus + ", xhr->status=" + xhr.statusText);
                            })

And in the categories.php handler for the .load() I currently have:

<?php 
ini_set('display_errors', 1);
error_reporting(E_ALL & ~E_NOTICE);


global $wpdb;
$query=("SELECT Cat2
         FROM wp_categories
         WHERE Cat3 IS NULL AND Cat4 IS NULL AND Cat5 IS NULL AND Cat6 IS NULL");
$dropdown = $wpdb->get_results($query, OBJECT);
print_r ($dropdown);
?>

But I'm getting an error from the $dropdown = line above: "Call to a member function get_results() on a non-object."

Does anyone see what I'm doing wrong?

Upvotes: 0

Views: 547

Answers (1)

Kirk Beard
Kirk Beard

Reputation: 9843

You're calling the /wp-content/themes/mydomain/categories.php file directly, rather than passing it through WordPress. This means the $wpdb object is not available for use.

You can confirm this by loading the categories.php file directly, and putting:

global $wpdb;
print_r($wpdb);

You won't get the WordPress Database object (unless you include the required files within categories.php directly.

You should be using the built-in WordPress Ajax functions for handling Ajax requests.

There are a lot of tutorials out there that explain how to use Ajax in WordPress:

Upvotes: 1

Related Questions