Hi I am not sure if the question is understandable or not but what I am trying to achieve I will explain. I have a custom post type called menu item and custom taxonomy called menu item type.
CODE
function create_menu_post_type()
{
$labels = array(
'name' => __( 'Food Menu','framework'),
'singular_name' => __( 'Menu Item','framework' ),
'add_new' => __('Add New','framework'),
'add_new_item' => __('Add New Menu Item','framework'),
'edit_item' => __('Edit Menu Item','framework'),
'new_item' => __('New Menu Item','framework'),
'view_item' => __('View Menu Item','framework'),
'search_items' => __('Search Menu Items','framework'),
'not_found' => __('No Menu Item found','framework'),
'not_found_in_trash' => __('No Menu Item found in Trash','framework'),
'parent_item_colon' => ''
);
$args = array(
'labels' => $labels,
'public' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
'menu_icon' => get_template_directory_uri() . '/images/menu.png',
'capability_type' => 'post',
'hierarchical' => false,
'menu_position' => 5,
'supports' => array('title','editor','thumbnail'),
'rewrite' => array( 'slug' => __('menu-item', 'framework') )
);
register_post_type('menu-item',$args);
}
add_action( 'init', 'create_menu_post_type' );
function create_menu_item_type_taxonomy(){
$labels = array(
'name' => __( 'Menu Item Types', 'framework' ),
'singular_name' => __( 'Menu Item Type', 'framework' ),
'search_items' => __( 'Search Menu Item Types', 'framework' ),
'popular_items' => __( 'Popular Menu Item Types', 'framework' ),
'all_items' => __( 'All Menu Item Types', 'framework' ),
'parent_item' => __( 'Parent Menu Item Type', 'framework' ),
'parent_item_colon' => __( 'Parent Menu Item Type:', 'framework' ),
'edit_item' => __( 'Edit Menu Item Type', 'framework' ),
'update_item' => __( 'Update Menu Item Type', 'framework' ),
'add_new_item' => __( 'Add New Menu Item Type', 'framework' ),
'new_item_name' => __( 'New Menu Item Type Name', 'framework' ),
'separate_items_with_commas' => __( 'Separate Menu Item Types with commas', 'framework' ),
'add_or_remove_items' => __( 'Add or Remove Menu Item Types', 'framework' ),
'choose_from_most_used' => __( 'Choose from the most used Menu Item Types', 'framework' ),
'menu_name' => __( 'Menu Item Types', 'framework' )
);
register_taxonomy(
'menu-item-type',
array( 'menu-item' ),
array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'query_var' => true,
'rewrite' => array('slug' => __('menu-item-type', 'framework'))
)
);
}
add_action( 'init', 'create_menu_item_type_taxonomy', 0 );
I have created a post named sushi under the term japenese. And then created post named chow mein under the term chinese.I want to populate archive posts by different terms. I have an archive page i.e archive.php where I am populating posts but my code populates all posts from all terms.
<?php $loop = new WP_Query( array( 'post_type' => 'menu-item', 'posts_per_page' => -1 ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post();
get_template_part( 'content', get_post_format() );
endwhile; wp_reset_query();
?>
<?php else : ?>
<?php get_template_part( 'content', 'none' ); ?>
<?php endif; ?>
I am navigating to the archive page via dynamic sidebar
<?php dynamic_sidebar( 'Archive' ); ?>
Please note that I have used the custom post type and the taxonomy code from a theme as a reference. Also if this is by any way a non-valid question feel free to comment and I will delete the question and try to find some other solution.
Adding a clearer example
Custom post -> menu-item
menu-item -> 2 posts -> sushi, chow mein
taxonomy -> menu-item-type
menu-item-type -> japenese, chinese
sushi -> japenese
chowmein -> chinese
Updated query
<?php
$current_term = get_term_by( 'slug', get_query_var('term') ,'menu-item-type' );
$loop = new WP_Query( array( 'post_type' => 'menu-item', 'posts_per_page' => -1, 'year' => $year_filter, 'tax_query' => array(
array( 'taxonomy' => 'menu-item-type', 'field' => 'slug', 'terms' => $current_term->name) ) ) ); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', get_post_format() );
endwhile; wp_reset_query();?>
<?php else : ?>
<div class="no-post"><?php _e( 'Sorry, no posts matched your criteria.' ); ?></div>
<?php endif; ?>