I've set up a custom taxonomy (menu_category) and custom post type (menu_item) in functions.php like this:
/* Set up custom post type for Menu Items */
function create_menu_items_post_type() {
$plugin_directory = plugins_url('images/', __FILE__ );
register_post_type( 'menu_item',
array(
'labels' => array(
'singular_name' => __( 'Menu item'),
'name' => __( 'The Menu'),
'add_new' => __( 'Add menu item'),
'add_new_item' => __( 'Add menu item'),
'edit_item' => __( 'Edit menu item'),
'new_item' => __( 'New menu item'),
'search_items' => __( 'Search menu items'),
'not_found' => __( 'No menu items found'),
'not_found_in_trash'=> __( 'No menu items found in trash'),
'all_items' => __( 'All menu items','sbr')
),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'menu_item'),
'publicly_queryable' => true,
'hierarchical' => true,
'show_ui' => true,
'show_in_menu' => true,
'exclude_from_search' => true,
'query_var' => true,
'menu_position' => 27,
'can_export' => true,
'menu_icon' => get_template_directory_uri() . '/images/system/menu.svg',
'supports' => array('title', 'revisions', 'author'),
'capability_type' => 'post',
'taxonomies' => array('menu_category'),
'capabilities' => array(
'create_posts' => 'edit_posts',
),
'map_meta_cap' => true,
)
);
}
add_action( 'init', 'create_menu_items_post_type' );
/* Set up custom taxonomy for Menu Categories */
function create_menu_category_taxonomy() {
$labels = array(
'name' => _x( 'Menu categories', 'taxonomy general name' ),
'singular_name' => _x( 'Menu category', 'taxonomy singular name' ),
'search_items' => __( 'Search menu categories' ),
'all_items' => __( 'All menu categories' ),
'parent_item' => __( 'Parent menu categories' ),
'parent_item_colon' => __( 'Parent menu categories:' ),
'edit_item' => __( 'Edit menu category' ),
'update_item' => __( 'Update menu category' ),
'add_new_item' => __( 'Add new menu category' ),
'new_item_name' => __( 'New menu category' ),
'menu_name' => __( 'Menu categories' ),
);
register_taxonomy(
'menu_category',
'menu_item',
array(
'label' => __( 'Menu Category' ),
'rewrite' => array( 'slug' => 'menu_category' ),
'hierarchical' => true,
'labels' => $labels
)
);
}
add_action( 'init', 'create_menu_category_taxonomy' );
...and that works fine when I'm in WP admin - I can add new categories, posts etc.
But on the front-end I'm trying to return a list of all the menu_category
categories and menu_item
posts like this:
$the_menus = get_categories(array(
'echo' => 0,
'hide_empty' => false,
'taxonomy' => 'menu_category',
'hierarchical' => 1,
'show_count' => 0
)); ?>
<?php
foreach ($the_menus as $the_menu) {
$the_menu_args = array(
'posts_per_page' => -1,
'post_type' => 'menu_item',
'showposts' => -1,
'post_status' => 'publish',
'cat' => $the_menu->cat_ID
);
$term = get_queried_object();
$the_menu_tasks = new WP_Query($the_menu_args);
$the_menu_slug = $the_menu->slug;
$the_menu_ID = $the_menu->cat_ID;
$page_slug = $term->slug;
?>
<li>
<a href="<?php echo get_category_link( $the_menu_ID ); ?>">
<?php echo $the_menu->cat_name; ?>
</a>
</li>
<?php
}
wp_reset_postdata();
The problem is this loop returns nothing.
If I remove 'taxonomy' => 'menu_category'
from $the_menus
and also remove 'post_type' => 'menu_item'
from $the_menu_args
, it returns all the normal categories and posts (you know, regular posts and categories).
So it seems it's only failing when I specify the custom taxonomy and custom post type.
What am I doing wrong?
PS: The 'menu items' mentioned here are for a restaurant menu I'm trying to build. It has nothing to do with the WordPress menu :-P