wp query - Why is my category template ignoring post type?

I am currently working on two sites that make extensive use of custom post types. For both we have a defined number of i

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?

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?

Share Improve this question asked Feb 18, 2016 at 17:23 jkd540jkd540 751 gold badge1 silver badge4 bronze badges 5
  • 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 Commented Feb 18, 2016 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 Commented Feb 18, 2016 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 Commented Feb 18, 2016 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 Commented Feb 18, 2016 at 19:19
  • 1 Possible duplicate of Template tags to display custom post type posts in category template? – Pieter Goosen Commented Feb 18, 2016 at 19:34
Add a comment  | 

1 Answer 1

Reset to default 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');

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744862581a4597790.html

相关推荐

  • wp query - Why is my category template ignoring post type?

    I am currently working on two sites that make extensive use of custom post types. For both we have a defined number of i

    1天前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信