4

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();

3 Answers 3

2

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

2

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:

3
  • Great alternative to the problem. +1 Jul 25, 2014 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... Jul 25, 2014 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, 2014 at 11:50
0

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.

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.