8

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/>';

    }
  • Try wrapping $wpdb->wp_posts with curly braces, ie. {$wpdb->wp_posts}.. – t31os Aug 19 '13 at 13:40
12
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/>';
}
  • hi @balamurugan, i tried your answer but im still not getting any results. you can see my Edit part above. – user1933824 Aug 19 '13 at 13:34
  • actually what u r getting and do u remove ... from my code. i tested it and getting all page id – Balas Aug 19 '13 at 13:45
  • im using my edit part as seen above my original post. i tried echo $result just to make sure that im retrieving data from the query what i get is print Array. when i use echo $page->ID i dont get anything. im really not sure why.. – user1933824 Aug 19 '13 at 13:52
  • you just simply copy & paste that code completely. That's all to do to get the result. – Balas Aug 19 '13 at 13:55
  • yes, it worked! when i try to review my code and yours, the only difference i saw is this part $tablename = $wpdb->prefix.'posts'; this part wasnt in the codex documentation. can you explain to me why it works? – user1933824 Aug 19 '13 at 13:59
3

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'" );
  • 1
    +1 for this, thank you. but i needed to give credit the person who responded to me first, he already provided the correct answer, i was just wasnt able to follow his instruction. – user1933824 Aug 19 '13 at 14:02
  • Sure. Sidenote: As I stated, the $wpdb->prefix shouldn't be used for built-in tables. Just call them directly. Fixed this is his answer as well. – kaiser Aug 19 '13 at 14:08
1

Try the following code. I faced the similar problem and solved it by removing $wpdb from 'FROM' field.

global $wpdb;
$result = $wpdb->get_results (
            "
            SELECT * 
            FROM  wp_posts 
            WHERE post_type =  'page'
            "
            );

echo $result; // display data
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.