Show only posts from one category on custom post type archive page

I have a custom post type, called Exercises. I also have many categories within that custom post type. I use archive-exercises.php custom loop to display my main exercises page.

Question: How do I modify my archive-exercises.php so it will display only post from specific category?

Read More

I managed to get similar effect on my home page with regular posts:

<?php query_posts('cat=93&amp;showposts='.get_option('posts_per_page')); ?>

Related posts

2 comments

  1. Use the pre_get_posts action to modify any main query before it is sent to the database, this includes the case of your home page as well. Calling query_posts in the template runs a new query, overwriting the original- it’s a waste of resources and can produce unpredictable results, particularly with pagination.

    function wpa_pre_get_posts( $query ){
        if( is_post_type_archive( 'exercises' ) && $query->is_main_query() ){
             $query->set( 'cat', 42 );
        }
    }
    add_action( 'pre_get_posts','wpa_pre_get_posts' );
    
  2. Just setup a new query restricted to your custom post type and your desired category/categories…

    $args = array(
        'post_type' => 'exercises',
        'cat' => 93,
        // some other parameters
    );
    $exercises = new WP_Query($args);
    while ($exercises->have_posts()) : $exercises->the_post() 
        // your posts stuff
    endwhile;
    

Comments are closed.