Add custom post types to the WordPress query

Run the filter whenever you want your post type in the main loop. Add ishome(), iscategory() etc. Example is for the post type 'paste'.

    add_filter( 'pre_get_posts', 'add_to_query' );

    function add_to_query( $query ) {
        // if ( is_home() ) {
            if( $query->query_vars['suppress_filters'] ) // TODO check if necessary
                return $query;
            $supported = $query->get( 'post_type' );
            if ( !$supported || $supported == 'post' )
                $supported = array( 'post', 'paste' );
            elseif ( is_array( $supported ) )
                array_push( $supported, 'paste' );
            $query->set( 'post_type', $supported );
            return $query;


  1. avatar
    wrote this comment on

    actually, i probably shouldn't just add 'post' to the query fwiw

  2. avatar
    wrote this comment on
    Thanks so much for this - sorted me right out. I had been trying some other pre_get_posts solutions offered up on the support forums but they were causing weird issues with sidebar WP_Query objects. This was the only way to get around it! Thanks!
  3. avatar
    wrote this comment on
    Glad it helped you :-)
  4. avatar
    wrote this comment on
    This really helped me out! It registered my CPT, as you can see > the right sidebar widget calls category CPT lesson Plans. Perfect. But I do not want a cross query happening >> I do not want the CPT to show in the blog, and I do not want blog posts to show up in the lesson plan sidebar. Right now the blog has all the lesson plans listed. Can I exclude them? I also had to alter your code a little bit - I had to include 'page' - because when I added your code all the page content disappeared. Will this code below work? Or have I missed something critical? (I took out 'post' because we are not searching 'posts' on most of the site - except in the blog area) thanks! Niki ____________________________________ aadd_filter( 'pre_get_posts', 'add_to_query' ); function add_to_query( $query ) { // if ( home() ) { if( $query->query_vars['suppress_filters'] ) // TODO check if necessary return $query; $supported = $query->get( 'post_type' ); if ( !$supported || $supported == 'post' ) $supported = array('post', 'port_plan', 'page' ); elseif ( is_array( $supported ) ) array_push( $supported, 'port_plan' ); $query->set( 'post_type', $supported ); return $query; //} }
  5. avatar
    wrote this comment on
    My code modifies the global query, which also displays your page. I assume you do a custom query in the sidebar? You can simply specify which post types to use then, see or the docs for the method you're using. You should not touch the global query for the sidebar.
  6. avatar
    wrote this comment on
    I'm very very close to making it work. ( ) ....we no longer need a second search for the blog. But I have 1 question. With this code in place - when I am in the dashboard and want to add a blog post, all of the custom-post-types show in that list. Is there a way to have them only display in the custom post type list - not tyhe list of regular posts? There will be a novice admin person managing the blog posts and I do not want to confuse her.
  7. avatar
    wrote this comment on
    Ah, you only want to run the filter on the frontend of your site. So add a conditional that checks for is_admin()
  8. avatar
    wrote this comment on
    wow - thank you so much for your contribution :) I added this>> if( !is_admin() ) worked like a charm. Thank you!
  9. avatar
    wrote this comment on
    no problem :-)


Cancel reply
Markdown. Syntax highlighting with <code lang="php"><?php echo "Hello, world!"; ?></code> etc.
DjangoPythonBitcoinTuxDebianHTML5 badgeSaltStackUpset confused bugMoneyHackerUpset confused bugX.OrggitFirefoxWindowMakerBashIs it worth the time?i3 window managerWagtailContainerIrssiNginxSilenceUse a maskWorldInternet securityPianoFontGnuPGThunderbirdJenkinshome-assistant-logo