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.

I'm a little stuck here. I have a menu in Wordpress with some posts in it. What I would like is to retrieve all the posts as objects.

I think I'm pretty close, but the SQL query doesn't seem to work as I want.

This is wat I have so far:

$querystr = "
SELECT wposts.* FROM wp_posts wposts
LEFT JOIN wp_postmeta wmeta ON wposts.ID = wmeta.post_id
LEFT JOIN wp_term_relationships wrel ON wrel.object_id = wmeta.meta_value
LEFT JOIN wp_terms wterms ON wrel.term_taxonomy_id = wterms.term_id
WHERE wterms.term_id= $menu_id
";

$posts = $wpdb->get_results($querystr, OBJECT);

I'm pretty sure it's a stupid little SQL logic error, but I realy can't find it..

share|improve this question
    
Please be more specific about your usecase. Maybe there're easier alternatives. –  kaiser Jun 17 '11 at 1:09
    
I think I am specific enough when I say I have a menu with some posts linked to. And in the code I need to retrieve all the posts, linked to that menu, as objects, so I can do some cool shit with them afterwards. As for the real reason why I need the posts and what I need to do with them, I don't think that's relevant, and would only make my question less usefull for other people –  Jules Jun 17 '11 at 12:49

2 Answers 2

up vote 3 down vote accepted

I recently needed the same thing, I had menu of categories and I needed to get the categories that in the menu. After a several hours digging in the WP core, I found the wp_get_nav_menu_items() function that helped me.

I finnally came with this, 220 was my menu_id

$navcat = wp_get_nav_menu_items('220');
    foreach ($navcat as $obj) {
        $catid = $obj->object_id;
        $category = get_category($catid);
        ...
        ...
    }

So, if you have a nav menu of posts, I assume you can do the same, something like this should work -

$nav_items = wp_get_nav_menu_items('220');  
    foreach ($nav_items as $obj) {
        $objid = $obj->object_id;
        $postobj = get_post($objid);

        //do stuff with the post..
    }

I think it can save you some complexed mySQL query...

share|improve this answer
    
thanks, not realy what i wanted, because you get a menu_item object back instead of the full post object. But for what I was trying to do it was sufficient. Especially since you can get the original post-id by getting $obj->object_id, allowing me to also get the posts meta whitout to much overhead. –  Jules Jun 17 '11 at 12:43
    
Oh, I read over this line $postobj = get_post($objid);. That would indeed give me the post, but that doesn't look very efficient, because there will be an extra query to the database fot each post, no? What I was trying is to get the posts in the most efficient way (= only one query) –  Jules Jun 17 '11 at 12:58
    
@Jules, you are right that in that way there will be a db query for each post. You can make it more efficient by something like this: foreach ($nav_items as $obj) { $objid = $obj->object_id; $postobjArr[] = $obj->object_id; //save array of post ids } $postobj = get_posts(array('post__in' => $postobjArr)); //get all posts in one time and do stuff with them.. –  Maor Barazany Jun 19 '11 at 14:46

There's a function to get a nav menu as object: wp_get_nav_menu_object() - the items (posts) are inside the object and accessible.

share|improve this answer
    
Thanks, that would probably have worked a bit like the wp_get_nav_menu_items() method Maor Barazany suggested. I haven't tried it because wp_get_nav_menu_items() is a bit more direct for what i need. –  Jules Jun 17 '11 at 12:46

protected by toscho Nov 10 '12 at 4:42

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

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