Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

1 Answer 1

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

 
discard

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.