19

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/>'; } 
1
  • Try wrapping $wpdb->wp_posts with curly braces, ie. {$wpdb->wp_posts}.. Commented Aug 19, 2013 at 13:40

4 Answers 4

30
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/>'; } 
6
  • 1
    hi @balamurugan, i tried your answer but im still not getting any results. you can see my Edit part above. Commented Aug 19, 2013 at 13:34
  • actually what u r getting and do u remove ... from my code. i tested it and getting all page id Commented Aug 19, 2013 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.. Commented Aug 19, 2013 at 13:52
  • you just simply copy & paste that code completely. That's all to do to get the result. Commented Aug 19, 2013 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? Commented Aug 19, 2013 at 13:59
5

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'" ); 
2
  • 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. Commented Aug 19, 2013 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. Commented Aug 19, 2013 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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.