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?

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')); ?>

2 Answers 2


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' );
  • After I added your code to functions.php, now there are no posts on my domain/exercises/, and when I try to see my posts under Exercises in the Admin back end, they all gone too... when I remove the code it all comes back
    – Acidon
    Commented Aug 17, 2013 at 21:33
  • ok I found that I have to check for !is_admin() to avoid the issue, however the page is still blank... I do have custom post types with a custom taxonomy, would that make a difference?
    – Acidon
    Commented Aug 17, 2013 at 22:09
  • ah sorry, didn't realize is_post_type_archive would be true on the admin side. is it a custom taxonomy and not the built in category taxonomy? you need to set tax_query parameters if that's the case, rather than cat.
    – Milo
    Commented Aug 17, 2013 at 23:33
  • Yes you are right, it is a custom taxonomy, so do I apply the tax_query parameters to pre_get_posts , or do I use one of the examples from the tax_query page and place it on my archive-exercises.php page?
    – Acidon
    Commented Aug 18, 2013 at 1:41
  • btw, thx for advising me to use pre_get_post on my homepage, it worked vonderfully, now I just need to figure out the tax_query :)
    – Acidon
    Commented Aug 18, 2013 at 4:36

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
  • I replaced <?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?> with your code above and get a blank page with no posts...
    – Acidon
    Commented Aug 17, 2013 at 21:50

Your Answer

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

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