I’m trying to retrieve information on my database. I wanted to display all pages
using this statement, but I’m getting a blank ARRAY
global $wpdb;
$result = $wpdb->get_results (
"
SELECT *
FROM $wpdb->wp_posts
WHERE post_type = 'page'
"
);
echo $result; // display data
Output:
ARRAY
EDIT: After changing below suggestions, I’m now using this. but I still don’t get any results:
global $wpdb;
$posts = $wpdb->wp_posts;
$result = $wpdb->get_results( " SELECT * FROM $posts WHERE 'post_type' = 'page' " );
foreach ($result as $page) {
echo $page->ID.'<br/>';
}
You have a slight misunderstanding:
When calling
$wpdb
, you get a list of properties that contain the core names of the tables:So your final query would look like this:
Try the following code. I faced the similar problem and solved it by removing $wpdb from ‘FROM’ field.
By “blank Array” do you mean an ’empty array’ or is the output ‘ARRAY’. If it’s the latter then, it is the expected output. You need to loop through that array and display results accordingly.
Reference: http://codex.wordpress.org/Class_Reference/wpdb#SELECT_Generic_Results