WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

how do I get all posts and postmetas in an associative array?

$allposts = $wpdb->get_results("
SELECT $wpdb->posts.post_title, $wpdb->postmeta.meta_key
FROM $wpdb->posts
JOIN $wpdb->postmeta 
ON $wpdb->postmeta.post_id = $wpdb->posts.id
WHERE $wpdb->posts.post_type = 'post' AND $wpdb->posts.post_status ='publish';", OBJECT_K);

I just don't get it....

share|improve this question

Basically, you can't do it in a single SQL query. You need two.

You don't have to make direct queries though, since you can use get_post() and get_post_custom().

share|improve this answer
hhmm..... but I would like to get all posts.. I thought get_post() is just for a specific one.. – joerg p May 22 '11 at 13:18
Right. When calling get_posts() or WP_Query, WP will automatically cache all the metadata, using a single query. You can then access it using get_post_custom() or get_post_meta() for each post. – scribu May 22 '11 at 15:10

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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