Wordpress - get_results using wpdb
global $wpdb;
$result = $wpdb->get_results ( "
SELECT *
FROM $wpdb->posts
WHERE post_type = 'page'
" );
foreach ( $result as $page )
{
echo $page->ID.'<br/>';
echo $page->post_title.'<br/>';
}
You have a slight misunderstanding:
When calling $wpdb
, you get a list of properties that contain the core names of the tables:
// The custom prefix from wp-config.php
// only needed for custom tables
$wpdb->prefix
// Tables where you don't need a prefix: built in ones:
$wpdb->posts
$wpdb->postmeta
$wpdb->users
So your final query would look like this:
$wpdb->get_results( "SELECT * FROM {$wpdb->posts} WHERE post_type = 'page'" );