1

I am currently working on two sites that make extensive use of custom post types. For both we have a defined number of items (cpts) that fall under a particular bucket (categories).

One of the sites is for a design shop, so the cpts are websites, logos, business cards, etc. And the categories are all the clients (several hundred).

Since the number of categories is quite large and always changing, I need to use a category.php template to display the collected projects (cpts) for a particular client.

I also need my cpts to display in a certain order--so websites in the top section of the page, logos in the bottom section, for example.

I initially created mulitple loops using query_posts & wp_reset_query. This got the posts to show where I wanted, but pagination was screwed up and after reading more I realized using query_posts is a big no-no.

However, whenever I use WP_Query it seems to completely ingnore my post_type argument (and only post_type). For example:

<?php
$current_cat = get_query_var('cat');
$args1 = array(
  'post_type' => 'event',
  'cat' => $current_cat,
  'offset' => 1
);

$query1 = new WP_Query( $args1 );

while ($query1->have_posts()) {
$query1->the_post();
echo '<li><a href="'.get_permalink( $post->ID).'">' . get_the_title() . '</a></li>';
} //endwhile;

?>      

The other thing is that I am only seeing this issue on the category template. When I use a loop similar to the above on a page template it returns the correct post types. What am I missing here?

  • Try breaking it down. E.g what will happen if you use only 'post_type' and remove 'cat' and 'offset'? Do you get the right results? – N00b Feb 18 '16 at 17:27
  • Have you considered the pre_get_posts filter to change the main query rather than adding a second? You should never use query_posts – Tom J Nowell Feb 18 '16 at 17:47
  • pre_get_posts is the only one I haven't tried. I know I should never use query_posts, but it actually returns what I want. I still have no idea why post_type is being ignored in the above. – jkd540 Feb 18 '16 at 19:09
  • @N00b: offset is only there to return all minus the latest. I can remove that with no consequences. Just a vanilla main query on my category.php will return only posts of that category (as expected). But for some reason when I add arguments such as number or post type I now have to use $current_cat or it returns all categories. Any idea why? – jkd540 Feb 18 '16 at 19:19
  • 1
0

Your issue is definitely caused by the lack of a pre_get_posts filter. Try this in your theme/functions.php:

// so event cpt is found on assigned cat archive page
function jdk540_event_query_post_type($query) {

    if ( ! is_admin() ) {

        if( is_category() || is_tag() &&  $query->is_main_query() && empty( $query->query_vars['suppress_filters'] ) ) {

            $post_type = get_query_var('post_type');

            if($post_type)
                $post_type = $post_type;
            else
                $post_type = array( 'post', 'event', 'nav_menu_item');

            $query->set('post_type', $post_type);
        }

    }

}
add_action('pre_get_posts', 'jdk540_event_query_post_type');
  • Shane, pre_get_posts is an action, not a filter. Check the source ;-) – Pieter Goosen Feb 19 '16 at 5:21
  • Corrected - ack. Why does it seem to work anyways with add_filter ? – shanebp Feb 19 '16 at 15:09
  • add_action just immediately calls add_filter, they both do exactly the same thing on that end. – Milo Feb 19 '16 at 16:21
  • Thanks, but no luck. I actually already had this in my functions.php. I can't figure this for the life of me. When I use a single vanilla query it returns exactly what I want (but not in the correct order) with category.php. As soon as I try to use WP_Query with multiple loops it completely ignores my post_type args. – jkd540 Feb 22 '16 at 19:47
  • To make things even more confusing I created multiple loops pulling different CPTs on a front-page.php this morning (for a different site) and it works perfectly. I just can't get the category archive to behave for some damn reason. – jkd540 Feb 22 '16 at 19:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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