Support » Plugins » Hacks » Custom Post Type placed in Category – Using pre_get_posts causes problems

  • Resolved accuratelimited

    (@accuratelimited)


    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)
  • John Parris

    (@mindctrl)

    See if is_main_query will do it for you. https://codex.wordpress.org/Function_Reference/is_main_query

    Thread Starter accuratelimited

    (@accuratelimited)

    Thanks for the suggestion. I got really excited when I saw it because it makes perfect sense.

    I’ve tried using it but it still doesn’t work, unfortunately. I am adding this in the function file of the theme as would be expected.

    I changed the first line to
    if( is_category() || is_tag() && $query->is_main_query() )
    and also tried:
    if( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) {

    Neither worked and still showed the posts in the sidebar widget.

    There is a workaround, of course: hiding visibility of the widget except on single posts / custom posts but I would prefer not to.

    Michael

    (@alchymyth)

    your conditional might be flawed, as the && only applies to is_tag() – try and use more brackets:

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

    (@accuratelimited)

    Alchymyth!!!! You are a lifesaver. That has worked like a treat! My mistake.

    Although only this worked as intended (incorporating both suggestions):

    if( (is_category() || is_tag()) && $query->is_main_query() ) {

    I then didn’t need the reference to the menu_item. In fact, I want it to apply to everything so can use this:

    `function arra_add_custom_types( $query ) {
    if( $query->is_main_query() ) {
    $query->set( ‘post_type’, array(
    ‘post’, ‘referee’
    ));
    return $query;
    }
    }
    add_filter( ‘pre_get_posts’, ‘arra_add_custom_types’ );’

    Thanks guys, that is the way forward. If anyone thinks it’ll screw up anything else please let me know.

    Thread Starter accuratelimited

    (@accuratelimited)

    The above function doesn’t work as it will attempt to return posts when you ask for a single page. It does need the is_category or is_archive if you prefer. So it should be:

    `function arra_add_custom_types( $query ) {
    if( (is_category() || is_tag()) && $query->is_main_query() ) {
    $query->set( ‘post_type’, array(
    ‘post’, ‘referee’
    ));
    return $query;
    }
    }
    add_filter( ‘pre_get_posts’, ‘arra_add_custom_types’ );’

    Obviously, if you wanted them to appear on your blog homepage you’d add “is_home() ||” before the is_category().

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.