I’m just getting my feet wet with creating a custom post type (which is up and running). I’d like to display the CPT alongside other posts on my homepage… I can’t figure it out for the life of me. I’m hoping someone can see the error of my ways before I pull more hair out. I’m sure it’s a rookie mistake, I have been staring at the screen perhaps a little too long… any help would be appreciated!
I’ve also come across a few articles suggesting pre_get_posts isn’t a good idea.
I’m using a Woothemes Canvas Child Theme
Have a page called Home that uses the Canvas Magazine Template
so…
function review_post() {
$labels = array(
'name'=> _x( 'Reviews', 'Post Type General Name', 'text_domain' ),
etc....
);
$rewrite = array(
'slug'=> 'review',
etc...
);
$args = array(
'label' => __( 'post_review_key', 'text_domain' ),
'description' => __( 'Posts for Review Snippets', 'text_domain' ),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'thumbnail', 'revisions', ),
'taxonomies' => array( 'reviews'),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 5,
'menu_icon' => '/review-star-16x16.jpg',
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'rewrite' => $rewrite,
'capability_type' => 'page',
);
register_post_type( 'post_review_key', $args );
}
// Hook into the 'init' action
add_action( 'init', 'review_post', 0 );
}
and then
add_action( 'pre_get_posts', 'add_my_post_types_to_query' );
function add_my_post_types_to_query( $query ) {
if ( is_home() && $query->is_main_query() )
$query->set( 'post_type', array( 'post', 'review_post' ) );
return $query;
}
Try this, should work:
Replace your code with both these code snippets and it will work.
review is the name of your CPT slug not review_post
Note: I have redone the code and changed the name and slug to reviews.
Plus you’re using the same function name review_post as the post_type which may cause problems. You have also added a taxonomy named the same
Here’s the code that works:
The above code goes in the custom function section of the functions.php file for the Canvas theme or a child themes functions file.
The code has been tested on the Canvas theme and also displays single cpts in the main posts page loop.