[wp-hackers] Select pages's associated menu items in a specific menu

Mike Schinkel mikeschinkel at newclarity.net
Wed Jul 28 02:25:54 UTC 2010


Lox,

Assuming I got the previous SQL correct I think this is what you are looking for:

http://gist.github.com/493195

The core of that code is here:

/* --------------------------- */
$post_id = $_GET['post'];
$menu_id = $_GET['menu'];

$term = get_term($menu_id, 'nav_menu');

$menu_items = new WP_Query(
	array(
		'post_type'   => 'nav_menu_item',
		'post_status' => 'any',
		'meta_key'    => '_menu_item_object_id',
		'meta_value'  => $post_id,
		'showposts'   => -1,
		'taxonomy'    => 'nav_menu',
		'term'        => $term->slug,
	)
);
/* --------------------------- */

The key thing to notice is that the menu item post records were related both via post meta (which you figured out) but also via the "nav_menu" taxonomy by term slug (which you hadn't included.)  At first I thought it wasn't going to be possible to do it with WP_Query() but after inspecting the SQL I wrote I realized it was indeed possible, at least what I understood that you needed.

Hope this helps.

-Mike


On Jul 27, 2010, at 10:02 PM, Mike Schinkel wrote:

> On Jul 27, 2010, at 8:14 PM, Lox wrote:
>> 2010/7/28 Mike Schinkel <mikeschinkel at newclarity.net>:
>>> So do you mean you have menus where a post may be referenced several times
>> 
>> Sure, but the problem is with posts that may be referenced several
>> times IN MULTIPLE MENUS.
> 
> I just wanted to understand what your requirements were.  (BTW, do you frequently have menu options that point to the same post? Hmm..)
> 
> I was struggling with what you wanted and also with the solution for what I think you are looking for so I decided to do it in "my native language" first, which would be SQL. :) 
> 
> Can you verify that this gives you the results you are looking for?
> 
> http://gist.github.com/493178
> 
> Copy the raw file to test.php in the root of your site and call it like this (replacing the URL params with your values of course):
> 
> http://example.com/test.php?menu=3&post=2
> 
> Let me know if this is ultimately what you want (I know it's better to use the WordPress API, but we'll get there...)
> 
> -Mike
> 
>  
> 



More information about the wp-hackers mailing list