Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

This should be simple, but it escapes me.

I have a custom post type and a custom taxonomy. The taxonomy is named "asset_type" and the slug is "type"

I am using wp_list_categories to provide a sidebar menu where users can click to view posts in the clicked taxonomy.

For example:

<div class="category-sidebar"><?php
            $args = array('post_type' => 'design_asset', 'show_count' => 1, 'taxonomy' => 'asset_type');
            wp_list_categories($args);
            ?>
</div>

This displays correctly my tax terms, for example, "element", with the link of http://site.dev/type/element/

It goes to the page, but I can't figure out how to modify my loop so it only shows posts that match the current term, in this example "element", nor display the title of the page as "Element".

My loop to display ALL the posts is:

$args = array( 'post_type' => 'design_asset', 'posts_per_page' => 100, 'orderby' => 'title', 'order' => 'ASC'  );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
share|improve this question

3 Answers 3

up vote 2 down vote accepted

Steve, you've asked a couple of questions which I had a look at, and I came to the conclusion that your loop is causing all of your headaches.

My loop to display ALL the posts is:

$args = array( 'post_type' => 'design_asset', 'posts_per_page' => 100, 'orderby' => 'title', 'order' => 'ASC'  );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();

Custom loops on any archive page creates problems. The main query is quite specific on these pages, and these queries are a shlep to reproduce in a custom query. To get an idea how the main query works and how the main query decides what to show where, go and check my answer on this question

I would advice you against using any custom query for your primary loop on any archive page, or even the home page.

My advice would be to change all your loops in all your templates back to the default loop

if ( have_posts() ) :
    while ( have_posts() ) : the_post();

       <----LOOP ELEMENTS---->

     endwhile;
 endif;

After you've changed back to the default loop, you'll see that everything will work normally, except your custom post type will not be included in your main loop.

To rectify this, you will use pre_get_posts to add your custom post type and other modification to the main query

function include_cpt( $query ) {
    if ( !is_admin() && $query->is_main_query() ) {
        $query->set( 'post_type', 'design_asset' );
        $query->set( 'posts_per_page', '100' );
        $query->set( 'orderby', 'title' );
        $query->set( 'order', 'ASC' ); 
    }
}
add_action( 'pre_get_posts', 'include_cpt' );

This should have everything working normal and as expected

share|improve this answer

It goes to the page, but I can't figure out how to modify my loop so it only shows posts that match the current term, in this example "element", nor display the title of the page as "Element".

The following arguments for your loop should do:

$args = array(
    'post_type' => 'design_asset',
    'posts_per_page' => 100,
    'orderby' => 'title',
    'order' => 'ASC',
    'tax_query' => array(
        array(
            'taxonomy' => 'asset_type',
            'field' => 'slug',
            'terms' => end( ( explode( '/', rtrim( $_SERVER['REQUEST_URI'], '/' ) ) ) )
        )
    )
);

References:

share|improve this answer
    
Great alternative to the problem. +1 –  Pieter Goosen Jul 25 at 5:17
1  
Not so sure whether "great". Yours, @Pieter (+1), is by a mile the more correct WP-like way of doing things. The above is the result of a tired, all-nighter mind. But yeah, it should work as expected... –  Johannes Pille Jul 25 at 5:53
    
Very nice, thanks. I guess my issue is that my original loop was from a Page template I made, which was always going to show only those posts. I've never really done any heavy lifting on the other WP templates like category, archive etc. I am trying to learn the differences and best practices, and this is a great start. –  Steve Jul 25 at 11:50

You've been bit by a common gremlin: wp_list_categories doesn't take post_type as an argument. wp_list_categories always generates links to categories for the default post_type 'post.' If you want to use categories separated by custom post types, you need to specify the relevant permalink. Here's an approach to adding a permalink structure for custom post type archive.

The answers above are helpful is you want your category archives to include all post types.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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