1
votes

I've create a custom post type called Manufactures and added a ton of posts and categories. The single post pages work but the category / archive page don't display any posts.

The Manufactures are broken into different categories, I need to display an archive of all the posts in each category. When I go to Manufactures>Category>Ge Speedtronic and click "view category" it takes me to the url listed below.

http://localhost/category/manufactures/ge-speedtronic/

This is where it gets confusing. The categories i'm using for the custom post type "Manufactures" also show up under my other custom post types and the default post section. So I created a category called Manufactures-Cat with sub categories. This might be causing the problem.

Any idea what posts aren't loading on the category page? Is it possible to create categories that only show up under a certain post type?

This code is in my functions.php

add_action( 'init', 'create_manufacturers' );
function create_manufacturers() {
  register_post_type( 'manufacturers',
    array(
      'labels' => array(
        'name' => __( 'Manufacturers' ),
        'singular_name' => __( 'Manufacturer' )
      ),
        'hierarchical'        => true,
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'menu_position'       => 5,
        'can_export'          => true,
        'has_archive'         => true,
        'has_category'         => true, 
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
        'capability_type'     => 'post',
        'taxonomies'  => array( 'category' ),
        'rewrite' => array('slug' => 'manufacturers'),
        'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' )
    )
  );
}

This is the code in my category.php file.

<?php get_header();?>
<div class="container page-category-wrap">
    <div class="row">
        <div class="col-md-9">
            <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
            <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                <div class="entry-content">
                    <h3><?php the_title(); ?></h3>
                    <?php the_content(); ?>
                </div>
            </article>
            <?php endwhile; endif; ?>
        </div>
        <div class="col-md-3">
            <?php dynamic_sidebar ('sidebar-1');?>
        </div>
    </div>
</div> 
<?php get_footer(); ?>
1

1 Answers

4
votes

You have to add your custom post type to the query object:

function my_query_post_type($query) {
    if ( is_category() && ( ! isset( $query->query_vars['suppress_filters'] ) || false == $query->query_vars['suppress_filters'] ) ) {
        $query->set( 'post_type', array( 'post', 'manufacturers' ) );
        return $query;
    }
}
add_filter('pre_get_posts', 'my_query_post_type');

https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts