Custom Post Type placed in Category – Using pre_get_posts causes problems
-
Hi Guys, This is a tough one.
I have created a custom post type, in this case, ‘referee’ for a referee website. And I want them to appear in category archives.
This is easy thanks to (https://css-tricks.com/snippets/wordpress/make-archives-php-include-custom-post-types/):
function namespace_add_custom_types( $query ) { if( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) { $query->set( 'post_type', array( 'post', 'nav_menu_item', 'my-custom-post-type' )); return $query; } } add_filter( 'pre_get_posts', 'namespace_add_custom_types' );
However, in the sidebar I want to display a list of the referees from another query. By using the above or similar code that suppresses filters, the code then returns normal posts that I don’t want when on a category page (i.e. Whilst it might luckily show a referee it is mostly normal posts).
<?php $args = array( 'post_type' => 'referee', 'posts_per_page' => $noc, 'post_status' => 'publish' ); $refereeshow = new WP_Query( $args ); while( $refereeshow->have_posts() ) : $refereeshow->the_post(); ?> <?php if ( has_post_thumbnail() ) { ?> <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_post_thumbnail( 'small-thumb', array('title' => get_the_title()) ); ?></a> <?php } else { ?> <a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title(); ?>"><img src="<?php echo get_template_directory_uri(); ?>/images/mini-thumbnail-default.jpg" alt="<?php the_title(); ?>" /></a> <?php } ?> <?php the_title( sprintf( '<h1><a href="%s" rel="bookmark">', esc_url( get_permalink() ) ), '</a></h1>' ); ?> <?php endwhile; wp_reset_query(); ?>
Is it possible to ensure either the pre-get-posts isn’t used on the second query or can anyone suggest another way to accomplish the task?
Viewing 5 replies - 1 through 5 (of 5 total)
Viewing 5 replies - 1 through 5 (of 5 total)
- The topic ‘Custom Post Type placed in Category – Using pre_get_posts causes problems’ is closed to new replies.