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..
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
So, if you have a nav menu of posts, I assume you can do the same, something like this should work –
I think it can save you some complexed mySQL query…
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.