Custom Posts on homepage

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.

Read More

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;
}

Related posts

2 comments

  1. 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' );
    
  2. 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.

Comments are closed.