wp_nav_menu_item_post_type_meta_box

Displays a meta box for a post type menu item.

Description

wp_nav_menu_item_post_type_meta_box( (string) $object, (array) $box ); 

Parameters (2)

0. $object (string)
The object.
1. $box (array)
Post type menu item meta box arguments.

Options

  • id (string) => ''

    Meta box id attribute.

  • title (string) => ''

    Meta box title.

  • callback (string) => ''

    Meta box display callback.

array(

    /**
     * Meta box 'id' attribute.
     *
     * @type string
     * @default ''
     */
    'id' => '',

    /**
     * Meta box title.
     *
     * @type string
     * @default ''
     */
    'title' => '',

    /**
     * Meta box display callback.
     *
     * @type string
     * @default ''
     */
    'callback' => ''
);        


Usage

  1. if ( !function_exists( 'wp_nav_menu_item_post_type_meta_box' ) ) { 
  2. require_once ABSPATH . '/wp-admin/includes/nav-menu.php'; 
  3.  
  4. // The object. 
  5. $object = ''; 
  6.  
  7. // Post type menu item meta box arguments. 
  8. $box = array( 
  9. 'id' => '', 
  10. 'title' => '', 
  11. 'callback' => '' 
  12. ); 
  13.  
  14. // NOTICE! Understand what this does before running. 
  15. $result = wp_nav_menu_item_post_type_meta_box($object, $box); 
  16.  

Defined (1)

The function is defined in the following location(s).

/wp-admin/includes/nav-menu.php  
  1. function wp_nav_menu_item_post_type_meta_box( $object, $box ) { 
  2. global $_nav_menu_placeholder, $nav_menu_selected_id; 
  3.  
  4. $post_type_name = $box['args']->name; 
  5.  
  6. // Paginate browsing for large numbers of post objects. 
  7. $per_page = 50; 
  8. $pagenum = isset( $_REQUEST[$post_type_name . '-tab'] ) && isset( $_REQUEST['paged'] ) ? absint( $_REQUEST['paged'] ) : 1; 
  9. $offset = 0 < $pagenum ? $per_page * ( $pagenum - 1 ) : 0; 
  10.  
  11. $args = array( 
  12. 'offset' => $offset,  
  13. 'order' => 'ASC',  
  14. 'orderby' => 'title',  
  15. 'posts_per_page' => $per_page,  
  16. 'post_type' => $post_type_name,  
  17. 'suppress_filters' => true,  
  18. 'update_post_term_cache' => false,  
  19. 'update_post_meta_cache' => false 
  20. ); 
  21.  
  22. if ( isset( $box['args']->_default_query ) ) 
  23. $args = array_merge($args, (array) $box['args']->_default_query ); 
  24.  
  25. // @todo transient caching of these results with proper invalidation on updating of a post of this type 
  26. $get_posts = new WP_Query; 
  27. $posts = $get_posts->query( $args ); 
  28. if ( ! $get_posts->post_count ) { 
  29. echo '<p>' . __( 'No items.' ) . '</p>'; 
  30. return; 
  31.  
  32. $num_pages = $get_posts->max_num_pages; 
  33.  
  34. $page_links = paginate_links( array( 
  35. 'base' => add_query_arg
  36. array( 
  37. $post_type_name . '-tab' => 'all',  
  38. 'paged' => '%#%',  
  39. 'item-type' => 'post_type',  
  40. 'item-object' => $post_type_name,  
  41. ),  
  42. 'format' => '',  
  43. 'prev_text' => '<span aria-label="' . esc_attr__( 'Previous page' ) . '">' . __( '«' ) . '</span>',  
  44. 'next_text' => '<span aria-label="' . esc_attr__( 'Next page' ) . '">' . __( '»' ) . '</span>',  
  45. 'before_page_number' => '<span class="screen-reader-text">' . __( 'Page' ) . '</span> ',  
  46. 'total' => $num_pages,  
  47. 'current' => $pagenum 
  48. )); 
  49.  
  50. $db_fields = false; 
  51. if ( is_post_type_hierarchical( $post_type_name ) ) { 
  52. $db_fields = array( 'parent' => 'post_parent', 'id' => 'ID' ); 
  53.  
  54. $walker = new Walker_Nav_Menu_Checklist( $db_fields ); 
  55.  
  56. $current_tab = 'most-recent'; 
  57. if ( isset( $_REQUEST[$post_type_name . '-tab'] ) && in_array( $_REQUEST[$post_type_name . '-tab'], array('all', 'search') ) ) { 
  58. $current_tab = $_REQUEST[$post_type_name . '-tab']; 
  59.  
  60. if ( ! empty( $_REQUEST['quick-search-posttype-' . $post_type_name] ) ) { 
  61. $current_tab = 'search'; 
  62.  
  63. $removed_args = array( 
  64. 'action',  
  65. 'customlink-tab',  
  66. 'edit-menu-item',  
  67. 'menu-item',  
  68. 'page-tab',  
  69. '_wpnonce',  
  70. ); 
  71.  
  72. ?> 
  73. <div id="posttype-<?php echo $post_type_name; ?>" class="posttypediv"> 
  74. <ul id="posttype-<?php echo $post_type_name; ?>-tabs" class="posttype-tabs add-menu-item-tabs"> 
  75. <li <?php echo ( 'most-recent' == $current_tab ? ' class="tabs"' : '' ); ?>> 
  76. <a class="nav-tab-link" data-type="tabs-panel-posttype-<?php echo esc_attr( $post_type_name ); ?>-most-recent" href="<?php if ( $nav_menu_selected_id ) echo esc_url(add_query_arg($post_type_name . '-tab', 'most-recent', remove_query_arg($removed_args))); ?>#tabs-panel-posttype-<?php echo $post_type_name; ?>-most-recent"> 
  77. <?php _e( 'Most Recent' ); ?> 
  78. </a> 
  79. </li> 
  80. <li <?php echo ( 'all' == $current_tab ? ' class="tabs"' : '' ); ?>> 
  81. <a class="nav-tab-link" data-type="<?php echo esc_attr( $post_type_name ); ?>-all" href="<?php if ( $nav_menu_selected_id ) echo esc_url(add_query_arg($post_type_name . '-tab', 'all', remove_query_arg($removed_args))); ?>#<?php echo $post_type_name; ?>-all"> 
  82. <?php _e( 'View All' ); ?> 
  83. </a> 
  84. </li> 
  85. <li <?php echo ( 'search' == $current_tab ? ' class="tabs"' : '' ); ?>> 
  86. <a class="nav-tab-link" data-type="tabs-panel-posttype-<?php echo esc_attr( $post_type_name ); ?>-search" href="<?php if ( $nav_menu_selected_id ) echo esc_url(add_query_arg($post_type_name . '-tab', 'search', remove_query_arg($removed_args))); ?>#tabs-panel-posttype-<?php echo $post_type_name; ?>-search"> 
  87. <?php _e( 'Search'); ?> 
  88. </a> 
  89. </li> 
  90. </ul><!-- .posttype-tabs --> 
  91.  
  92. <div id="tabs-panel-posttype-<?php echo $post_type_name; ?>-most-recent" class="tabs-panel <?php 
  93. echo ( 'most-recent' == $current_tab ? 'tabs-panel-active' : 'tabs-panel-inactive' ); 
  94. ?>"> 
  95. <ul id="<?php echo $post_type_name; ?>checklist-most-recent" class="categorychecklist form-no-clear"> 
  96. <?php 
  97. $recent_args = array_merge( $args, array( 'orderby' => 'post_date', 'order' => 'DESC', 'posts_per_page' => 15 ) ); 
  98. $most_recent = $get_posts->query( $recent_args ); 
  99. $args[walker] = $walker; 
  100.  
  101. /** 
  102. * Filters the posts displayed in the 'Most Recent' tab of the current 
  103. * post type's menu items meta box. 
  104. * The dynamic portion of the hook name, `$post_type_name`, refers to the post type name. 
  105. * @since 4.3.0 
  106. * @param array $most_recent An array of post objects being listed. 
  107. * @param array $args An array of WP_Query arguments. 
  108. * @param array $box Arguments passed to wp_nav_menu_item_post_type_meta_box(). 
  109. */ 
  110. $most_recent = apply_filters( "nav_menu_items_{$post_type_name}_recent", $most_recent, $args, $box ); 
  111.  
  112. echo walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', $most_recent), 0, (object) $args ); 
  113. ?> 
  114. </ul> 
  115. </div><!-- /.tabs-panel --> 
  116.  
  117. <div class="tabs-panel <?php 
  118. echo ( 'search' == $current_tab ? 'tabs-panel-active' : 'tabs-panel-inactive' ); 
  119. ?>" id="tabs-panel-posttype-<?php echo $post_type_name; ?>-search"> 
  120. <?php 
  121. if ( isset( $_REQUEST['quick-search-posttype-' . $post_type_name] ) ) { 
  122. $searched = esc_attr( $_REQUEST['quick-search-posttype-' . $post_type_name] ); 
  123. $search_results = get_posts( array( 's' => $searched, 'post_type' => $post_type_name, 'fields' => 'all', 'order' => 'DESC', ) ); 
  124. } else { 
  125. $searched = ''; 
  126. $search_results = array(); 
  127. ?> 
  128. <p class="quick-search-wrap"> 
  129. <label for="quick-search-posttype-<?php echo $post_type_name; ?>" class="screen-reader-text"><?php _e( 'Search' ); ?></label> 
  130. <input type="search" class="quick-search" value="<?php echo $searched; ?>" name="quick-search-posttype-<?php echo $post_type_name; ?>" id="quick-search-posttype-<?php echo $post_type_name; ?>" /> 
  131. <span class="spinner"></span> 
  132. <?php submit_button( __( 'Search' ), 'small quick-search-submit hide-if-js', 'submit', false, array( 'id' => 'submit-quick-search-posttype-' . $post_type_name ) ); ?> 
  133. </p> 
  134.  
  135. <ul id="<?php echo $post_type_name; ?>-search-checklist" data-wp-lists="list:<?php echo $post_type_name?>" class="categorychecklist form-no-clear"> 
  136. <?php if ( ! empty( $search_results ) && ! is_wp_error( $search_results ) ) : ?> 
  137. <?php 
  138. $args[walker] = $walker; 
  139. echo walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', $search_results), 0, (object) $args ); 
  140. ?> 
  141. <?php elseif ( is_wp_error( $search_results ) ) : ?> 
  142. <li><?php echo $search_results->get_error_message(); ?></li> 
  143. <?php elseif ( ! empty( $searched ) ) : ?> 
  144. <li><?php _e('No results found.'); ?></li> 
  145. <?php endif; ?> 
  146. </ul> 
  147. </div><!-- /.tabs-panel --> 
  148.  
  149. <div id="<?php echo $post_type_name; ?>-all" class="tabs-panel tabs-panel-view-all <?php 
  150. echo ( 'all' == $current_tab ? 'tabs-panel-active' : 'tabs-panel-inactive' ); 
  151. ?>"> 
  152. <?php if ( ! empty( $page_links ) ) : ?> 
  153. <div class="add-menu-item-pagelinks"> 
  154. <?php echo $page_links; ?> 
  155. </div> 
  156. <?php endif; ?> 
  157. <ul id="<?php echo $post_type_name; ?>checklist" data-wp-lists="list:<?php echo $post_type_name?>" class="categorychecklist form-no-clear"> 
  158. <?php 
  159. $args[walker] = $walker; 
  160.  
  161. /** 
  162. * If we're dealing with pages, let's put a checkbox for the front 
  163. * page at the top of the list. 
  164. */ 
  165. if ( 'page' == $post_type_name ) { 
  166. $front_page = 'page' == get_option('show_on_front') ? (int) get_option( 'page_on_front' ) : 0; 
  167. if ( ! empty( $front_page ) ) { 
  168. $front_page_obj = get_post( $front_page ); 
  169. $front_page_obj->front_or_home = true; 
  170. array_unshift( $posts, $front_page_obj ); 
  171. } else { 
  172. $_nav_menu_placeholder = ( 0 > $_nav_menu_placeholder ) ? intval($_nav_menu_placeholder) - 1 : -1; 
  173. array_unshift( $posts, (object) array( 
  174. 'front_or_home' => true,  
  175. 'ID' => 0,  
  176. 'object_id' => $_nav_menu_placeholder,  
  177. 'post_content' => '',  
  178. 'post_excerpt' => '',  
  179. 'post_parent' => '',  
  180. 'post_title' => _x('Home', 'nav menu home label'),  
  181. 'post_type' => 'nav_menu_item',  
  182. 'type' => 'custom',  
  183. 'url' => home_url('/'),  
  184. ) ); 
  185.  
  186. $post_type = get_post_type_object( $post_type_name ); 
  187.  
  188. if ( $post_type->has_archive ) { 
  189. $_nav_menu_placeholder = ( 0 > $_nav_menu_placeholder ) ? intval($_nav_menu_placeholder) - 1 : -1; 
  190. array_unshift( $posts, (object) array( 
  191. 'ID' => 0,  
  192. 'object_id' => $_nav_menu_placeholder,  
  193. 'object' => $post_type_name,  
  194. 'post_content' => '',  
  195. 'post_excerpt' => '',  
  196. 'post_title' => $post_type->labels->archives,  
  197. 'post_type' => 'nav_menu_item',  
  198. 'type' => 'post_type_archive',  
  199. 'url' => get_post_type_archive_link( $post_type_name ),  
  200. ) ); 
  201.  
  202. /** 
  203. * Filters the posts displayed in the 'View All' tab of the current 
  204. * post type's menu items meta box. 
  205. * The dynamic portion of the hook name, `$post_type_name`, refers 
  206. * to the slug of the current post type. 
  207. * @since 3.2.0 
  208. * @since 4.6.0 Converted the `$post_type` parameter to accept a WP_Post_Type object. 
  209. * @see WP_Query::query() 
  210. * @param array $posts The posts for the current post type. 
  211. * @param array $args An array of WP_Query arguments. 
  212. * @param WP_Post_Type $post_type The current post type object for this menu item meta box. 
  213. */ 
  214. $posts = apply_filters( "nav_menu_items_{$post_type_name}", $posts, $args, $post_type ); 
  215.  
  216. $checkbox_items = walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', $posts), 0, (object) $args ); 
  217.  
  218. if ( 'all' == $current_tab && ! empty( $_REQUEST['selectall'] ) ) { 
  219. $checkbox_items = preg_replace('/(type=(.)checkbox(\2))/', '$1 checked=$2checked$2', $checkbox_items); 
  220.  
  221.  
  222. echo $checkbox_items; 
  223. ?> 
  224. </ul> 
  225. <?php if ( ! empty( $page_links ) ) : ?> 
  226. <div class="add-menu-item-pagelinks"> 
  227. <?php echo $page_links; ?> 
  228. </div> 
  229. <?php endif; ?> 
  230. </div><!-- /.tabs-panel --> 
  231.  
  232. <p class="button-controls wp-clearfix"> 
  233. <span class="list-controls"> 
  234. <a href="<?php 
  235. array( 
  236. $post_type_name . '-tab' => 'all',  
  237. 'selectall' => 1,  
  238. ),  
  239. remove_query_arg( $removed_args ) 
  240. )); 
  241. ?>#posttype-<?php echo $post_type_name; ?>" class="select-all aria-button-if-js"><?php _e( 'Select All' ); ?></a> 
  242. </span> 
  243.  
  244. <span class="add-to-menu"> 
  245. <input type="submit"<?php wp_nav_menu_disabled_check( $nav_menu_selected_id ); ?> class="button submit-add-to-menu right" value="<?php esc_attr_e( 'Add to Menu' ); ?>" name="add-post-type-menu-item" id="<?php echo esc_attr( 'submit-posttype-' . $post_type_name ); ?>" /> 
  246. <span class="spinner"></span> 
  247. </span> 
  248. </p> 
  249.  
  250. </div><!-- /.posttypediv --> 
  251. <?php