Hi WordPress Experts please help me i want to get nav_menu_item_id and slug from wp database by given a page template like (store.php) this a page template and how we can get nave menu item id and slug that use this page template


Hi @Anjum:

I think the function get_nav_menu_id_by_page_template()is what you are looking for? You can place the code for this function in your theme's functions.php file, or in a .php for a plugin you are writing:

function get_nav_menu_id_by_page_template($template) {
  static $nav_menu_id;
  if (!isset($nav_menu_id)) {
    global $wpdb;
    $sql = <<<SQL
  menu.post_id AS menu_id
  {$wpdb->postmeta} AS template 
  INNER JOIN {$wpdb->postmeta} AS menu_id ON menu_id.meta_key='_menu_item_object_id' 
                                         AND menu_id.meta_value=template.post_id
  INNER JOIN {$wpdb->postmeta} AS menu ON menu_id.post_id=menu.post_id
  AND menu.meta_key='_menu_item_object' AND menu.meta_value='page'
  AND template.meta_key='_wp_page_template' AND template.meta_value='%s'
    $nav_menu_id = $wpdb->get_var($wpdb->prepare($sql,$template));
  return $nav_menu_id;

Once you have the get_nav_menu_id_by_page_template() function in your code you can call it in your theme template file like so:

<?php $nav_menu_id = get_nav_menu_id_by_page_template('store.php'); ?>
  • I wondered if it would be faster to extend the nav menu query and let it collect the required information in an array which is accessible from the outside. +1 anyway. :) – fuxia Mar 28 '11 at 4:42
  • @MikeSchinkel i am getting an error with it Parse error: syntax error, unexpected T_SL – Anjum Mar 28 '11 at 5:11
  • @Anjum - Didi you copy my code exactly? Do you accidentally indent or put a space in front of SQL;? – MikeSchinkel Mar 28 '11 at 5:45
  • @MikeSchinkel i have copied and paste your code as it is in my function.php but i am getting this error any other way to.... – Anjum Mar 28 '11 at 12:24
  • @Anjum - Can you give me the entire error message? File name, Line number and all? – MikeSchinkel Mar 29 '11 at 7:16

Multiple pages may use the same template. There is no 1:1 relation between templates and pages or nav menu items, so you may get more than one item back.

Templates and item IDs are stored in the table postmeta. Look for _menu_item_object_id and _wp_page_template.

  • i am using special template (store.php) for my store (shop) page how can i get menu item id and slug by this template – Anjum Mar 27 '11 at 11:26

function getIdFromMeta( $meta_key, $meta_value ) {
    global $wpdb;
    $pid = $wpdb->get_var( $wpdb->prepare("SELECT post_id FROM $wpdb->postmeta WHERE meta_value = '$meta_value' AND meta_key = '$meta_key' ORDER BY post_id DESC") );
    if( $pid != '' )
        return $pid;
        return false;

it is work fine for me this solution give me Rochester Oliveira my question on stackoverflow Thanks Rochester Oliveira

this return post id but i needed nav menu id

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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