Adding a custom post type to category view

Hi,
I have added a new custom post type to my init.php file, and I see the new type in my admin area.

if ( ! function_exists('custom_products') ) {

// Register Custom Post Type
function custom_products() {

	$labels = array(
		'name'                => _x( 'מוצרים', 'Post Type General Name', 'text_domain' ),
		'singular_name'       => _x( 'מוצרים', 'Post Type Singular Name', 'text_domain' ),
		'menu_name'           => __( 'מוצרים', 'text_domain' ),
		'parent_item_colon'   => __( 'פריט אב', 'text_domain' ),
		'all_items'           => __( 'כל המוצרים', 'text_domain' ),
		'view_item'           => __( 'הצג מוצר', 'text_domain' ),
		'add_new_item'        => __( 'הוסף מוצר חדש', 'text_domain' ),
		'add_new'             => __( 'הוסף מוצר', 'text_domain' ),
		'edit_item'           => __( 'ערוך צוצר', 'text_domain' ),
		'update_item'         => __( 'עדכן מוצר', 'text_domain' ),
		'search_items'        => __( 'בחר מוצר', 'text_domain' ),
		'not_found'           => __( 'לא נמצא', 'text_domain' ),
		'not_found_in_trash'  => __( 'לא נמצא באשפה', 'text_domain' ),
	);
	$args = array(
		'label'               => __( 'product', 'text_domain' ),
		'description'         => __( 'מוצרים', 'text_domain' ),
		'labels'              => $labels,
		'supports'            => array( 'title', 'editor', 'excerpt', 'thumbnail', 'trackbacks', 'custom-fields', 'page-attributes', 'post-formats', ),
		'taxonomies'          => array( 'category', 'post_tag' ),
		'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'           => '',
		'can_export'          => true,
		'has_archive'         => true,
		'exclude_from_search' => false,
		'publicly_queryable'  => true,
		'capability_type'     => 'post',
	);
	register_post_type( 'product', $args );

}

// Hook into the 'init' action
add_action( 'init', 'custom_products', 0 );

}   

Tried to add a few posts and see it in the category page or in a single post, but it seems like roots can not recognize the new type.
How can I add the new type to my template? I want to see the new type belong a regular posts in the category page & of course I want to allow a single-post view for my new type.
Thank you.

Roots doesn’t change the way Custom Post Types are implemented, so this question would be better suited to WordPress “Development” of one of its equivalents.

It could just be your rewrite rules need flushing (visit the permalinks page) and it’s always worth switching to a default theme and back again to see if it’s only a problem in Roots. Also try viewing the post from the admin screen, as that link will have the correct slug.

Thank you!
(For future searches:) Solved by adding filter to init.php

add_filter( 'pre_get_posts', 'my_get_posts' );

function my_get_posts( $query ) {
    
    $current_post_type = $query->get('post_type');
        
    if($current_post_type == 'post' || $current_post_type == '')
        $query->set( 'post_type', array( 'post', 'product') );
    
    return $query;
}
2 Likes

+1 for posting your fix. I was going to suggest something similar.

I’ve been having similar problems, the code above worked and the custom posts are showing on the blog page, however, the categories don’t show up. Does anyone know how to fix that? They show in ‘recent posts’.

Any help would be much appreciated. x