0

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; ?>
3
  • I think you mean terms when you refer to japanese and chinese. Just make sure whether they are terms or actual taxonomies, as your termonology is confusing Aug 27, 2014 at 11:30
  • I have registered a taxonomy 'menu-item-type' and that are japanese and chinese. I am getting into wordpress and I am sorry if I said anything wrong.
    – fahad.kazi
    Aug 27, 2014 at 11:38
  • I think I wasn't aware of the fact that chinese and japenese are terms so thanks @PieterGoosen. Taxonomy is menu-item-type
    – fahad.kazi
    Aug 27, 2014 at 12:27

1 Answer 1

1

If I understand you correctly, you need to show all posts from a term when a term's page is being displayed.

Before I answer the real question, there are a few points I would like to highlight since you are new to Wordpress

  • Never use hyphens (-) to separate names in custom post type names and custom taxonomy names. They are extremely troublesome later, specially when it comes to custom templates. If you really need to separate names, only use underscores (_)

  • Don't create multiple instances that are hooked to the same hook. Add all relevant functions in one function and add that one function to the desired hook

OK, now for the real problem. Your problem is the custom query. You should never change the main query for a custom query on any archive type page or the home page. If you need to manipulate the main query on these pages, rather use pre_get_posts to do so.

To solve your problem, just go back to the default loop on archive.php. You can also create a taxonomy.php template which will be used instead for custom taxonomies

Here is an example of how your archive or taxonomy page should look like

<?php

get_header(); ?>

    <section id="primary" class="content-area">
        <div id="content" class="site-content" role="main">

            <?php if ( have_posts() ) : ?>


            <?php
                    // Start the Loop.
                    while ( have_posts() ) : the_post();

                        get_template_part( 'content', get_post_format() );

                    endwhile;
                    // Previous/next page navigation.

                    //YOUR PAGINATION FUNCTIONS;

                else :
                    // If no content, include the "No posts found" template.
                    get_template_part( 'content', 'none' );

                endif;
            ?>
        </div><!-- #content -->
    </section><!-- #primary -->

<?php
get_sidebar();
get_footer();

ADDITIONAL READING

EDIT

Just on your updated code, there are two problems. (I still don't see why you are not making use of the above solution, you are over-complicating things here)

  • In your tax_query, you are retrieving the term by slug, yet you are feeding the term name to it. You will need to change your field to name

  • When using WP_Query, you will need to make use of wp_reset_postdata() to reset your query, not wp_reset_query() as this is used in conjuction with query_posts

4
  • Sorry for replying this late but I updated my filter for wp_query check the updated code.
    – fahad.kazi
    Oct 30, 2014 at 7:37
  • Did you try my solution. Also, you will need to add 'has_achive' => true; to your custom post type arguments Oct 30, 2014 at 7:44
  • Honestly speaking I did not write the code. My senior wrote the code as I am still learning things on customizing things. From what I understand about wp_query is you can filter things in it. And about archive page we did not create any page as the design had years name in the sidebar. He wrote some code with which when you select a year posts populate of that particular term.
    – fahad.kazi
    Oct 30, 2014 at 8:05
  • It does seem that this is as far as I can help. Your structures are completely unknown to me. I have answered based on what is given. Oct 30, 2014 at 8:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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