1

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;
}
7
  • 1
    Shouldn't it be $query->set( 'post_type', array( 'post', 'post_review_key' ) ) ?
    – Shazzad
    Feb 9, 2014 at 17:14
  • I tried that, didn't work for me.
    – alexwc_
    Feb 9, 2014 at 17:28
  • If you're homepage is using frontpage.php you will need to use is_front_page() instead of is_home(). Also if you want to display a separate query of a custom post type, read into WP_Query
    – Howdy_McGee
    Feb 9, 2014 at 17:34
  • if you're using a static page with a custom template, it's likely the query which loads your posts is not the main query, so a hook on pre_get_posts won't work in this case. you'll have to look at the template to see how that query is being performed, you'll probably have to override the template in your child theme and modify the query directly within the template. this is probably a question for woo support anyway.
    – Milo
    Feb 9, 2014 at 18:09
  • I think he just wants to include cpts on his post page with regular posts. Feb 9, 2014 at 18:43

2 Answers 2

0

Try this, should work:

function add_my_post_types_to_query( $query ) {
    if ( ( is_home() || is_front_page() ) && $query->is_main_query() ) {
        $query->set( 'post_type', array( 'post', 'post_review_key' ) );
    }
}
add_action( 'pre_get_posts', 'add_my_post_types_to_query' );
0

Replace your code with both these code snippets and it will work.

add_action( 'pre_get_posts', 'add_custom_post_types_to_loop' );

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

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:

add_action( 'init', 'new_post_type' );
function new_post_type() {

register_post_type( 'reviews',
    array(
        'labels' => array(
            'name'          => __( 'Reviews', 'wpsites' ),
            'singular_name' => __( 'Review', 'wpsites' ),
        ),
'label'               => __( 'Reviews', 'text_domain' ),
'description'         => __( 'Posts for Review Snippets', 'text_domain' ),
'labels'              => $labels,
'supports'            => array( 'title', 'editor', 'thumbnail', 'revisions', ),
'taxonomies'          => array( 'review-type' ),
'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'           => 'dashicons-images-alt2',
'can_export'          => true,
'has_archive'         => true,
'exclude_from_search' => false,
'publicly_queryable'  => true,
'rewrite'      => array( 'slug' => 'reviews', 'with_front' => false ),
'capability_type'     => 'page',

));

}

enter image description here

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.

5
  • Please add an explanation to your answer: why could that solve the problem?
    – fuxia
    Feb 9, 2014 at 18:34
  • great! Thanks Brad. This works if I have Setttings >> Reading >> Front Page Displays Latest Posts. What aiming for is to have a static front page (i'll just call it "Test Page" for now) that uses the Magazine template that will show all posts, CPTs included. I edited my original post to reflect this about 20min after I posted.
    – alexwc_
    Feb 9, 2014 at 20:00
  • It does. I tested it using the Magazine template on the front page. Feb 9, 2014 at 20:14
  • hmm... it doesn't work for me. Perhaps I missed a step: I have Front Page Displays: A Static Page (page = Test Page - w/ Magazine Template), and no page chosen for posts. Only normal posts show (on the page and in Recent Posts widget, which perhaps is a different issue altogether). If I've missed a step, that might explain. Thanks so far!
    – alexwc_
    Feb 10, 2014 at 4:02
  • You need to select a page to display posts Feb 10, 2014 at 9:00

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.