2

Is there a way to force Custom Post Types to behave as normal wp posts?

Hi - I need some help adding a custom post type to my theme's posts section. The posts page is a static page using a template (not the standard wp home/blog page). The template allows me to select post categories to feature on the page, but my custom post types never show up in the choices.

If I select "show all posts" the custom posts still don't show up on the published page unless I add the following code to the end of my functions.php

My custom posts then show up in among the regular posts (as I wanted) BUT, it messes up the layout of the site (the main nav menu disappears!)

add_filter( 'pre_get_posts', 'my_get_posts' );
function my_get_posts( $query ) {
    $query->set( 'post_type', array( 'post', 'page', 'projects' ) );
return $query;
}
3

1 Answer 1

3

The reason its messing up is because its adding the custom post type everywhere. You want to limit it to just the main loop of the main page. Justin Tadlock wrote an article on how to do that here. Here is what your code should look like:

add_filter( 'pre_get_posts', 'my_get_posts' );

function my_get_posts( $query ) {    
    if ( is_home() && $query->is_main_query() )
         $query->set( 'post_type', array( 'post', 'page', 'projects' ) );    
    return $query;
}

This limits it to just the main loop of the home page.

3
  • Thanks for that Manny. I am aware of JT's article and started off there, but my posts page is not the standard wp "home" page. The only code I found that would list my CPTs along with normal posts is the code I'm using just now Feb 12, 2014 at 17:34
  • Does your posts page use the main loop? Just remove the is_home() part of it Feb 12, 2014 at 17:50
  • Thanks, I had tried that, but the result is still the same as the code I started with - posts show, but navigation disappears. I think the answer will lie in altering the template (link above) ?? Feb 12, 2014 at 18:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.