WordPress.org

Ready to get started?Download WordPress

Forums

Display Posts Shortcode
[resolved] Goes haywire when I force CPT to display on Category pages (3 posts)

  1. LucasHall
    Member
    Posted 1 year ago #

    Hi Bill,

    When I add this code (see below) to my functions file, the Display-Posts-Shortcode output seems to go haywire. Meaning: it no longer pulls posts from the right Category, Type, Tag, etc, and instead shows a mix of pages and posts.

    Using this code, I'm able to display my CPT posts on my normal blog category pages - mixed in with my other articles. But the DPS plugin on those page stops filtering the right posts

    Do you see anything in this code that would conflict with your plugin?

    Example Category page: http://landlordology.com/category/beginners/
    Note the "Landlord Tips" widget on the right is DPS (5th down) - and not working correctly. This category page has CPT (tips) and posts sharing the same page.

    P.S. I'm also running Genesis 1.9.2

    Best,
    Lucas

    /************************************************
     Add Custom Post Types to Category Pages - This causes issues on with DISPLAY POSTS SHORTCODES on category pages
    ************************************************/
     function namespace_add_custom_types( $query ) {
      if( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) {
        $query->set( 'post_type', array(
         'post', 'tips', 'nav_menu_item'
    		));
    	  return $query;
    	}
     }
    
     add_filter( 'pre_get_posts', 'namespace_add_custom_types' );

    http://wordpress.org/extend/plugins/display-posts-shortcode/

  2. Bill Erickson
    Member
    Plugin Author

    Posted 1 year ago #

    Not only will that conflict with Display Posts Shortcode, it should also conflict with anything else that's doing a query on the page (your menu, a recent posts widget...).

    You need to include $query->is_main_query() in your if statement. This ensures your code only runs on the main query (the listing of posts on that page).

    For more information, read this article: http://www.billerickson.net/customize-the-wordpress-query/

  3. LucasHall
    Member
    Posted 1 year ago #

    Bill,

    You're a rockstar! Adding $query->is_main_query() to my if statement worked like a charm. My statement now reads
    if( $query->is_main_query() && is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) {

    Thank you again!
    Lucas

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic