Question

In the code below (from my functions.php), I'm attempting to create an array of items from the wp_postmeta table where the meta_key is "_wp_attached_file". I'm getting an error:

Fatal error: Call to a member function query() on a non-object

What's wrong with the query?

$excludeImages = array();
$excludeImages = $wpdb->query("SELECT meta_value 
                              FROM $wpdb->postmeta 
                              WHERE meta_key = '_wp_attached_file'");

array_push($excludeImages);
Was it helpful?

Solution

Hi @Scott B:

Did you remember to include a global $wpdb; in your code, like so?

global $wpdb;
$excludeImages = array();
$excludeImages = $wpdb->query("SELECT meta_value 
                              FROM $wpdb->postmeta 
                              WHERE meta_key = '_wp_attached_file'");

array_push($excludeImages);
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top