WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Custom post types not showing on category archive pages (9 posts)

  1. SchuminWeb
    Member
    Posted 8 months ago #

    Am having a problem with my custom post types on a website I'm building. I have a custom post type called "article" on the site, and it's configured to use categories. However, when I go to the categories where these custom posts are assigned, I get a "not found" error.

    The site does not use the regular posts. However, when I created one as a test in the course of troubleshooting this problem, the archive page for the category that I placed the regular post into started working. Any categories that don't contain the test post still don't work. And the "article" posts don't show up in the category archive. When the regular post is in the category, it is the only thing that shows up in the category.

    However, the custom posts go into the categories just fine on the dashboard, and when I use the "collapsing categories" plugin widget, everything works great. It's just the category archive page that doesn't work.

    This is how I registered the custom post type:

    <?php add_action( 'init', 'create_my_post_types' );
    
    function create_my_post_types() {
    	register_post_type( 'article',
    		array(
    			'labels' => array(
    				'name' => __( 'Articles' ),
    				'singular_name' => __( 'Article' ),
    				'add_new_item' => __( 'Add New Article' ),
    				'edit_item' => __( 'Edit Article' )
    			),
    			'show_ui' => true,
    			'public' => true,
    			'has_archive' => true,
    			'supports' => array('title', 'editor', 'author', 'custom-fields', 'thumbnail', 'comments', 'revisions', 'trackbacks'),
    			'taxonomies' => array('category'),
    		)
    	);
    }
    ?>

    What am I doing wrong?

  2. blacklinemedia
    Member
    Posted 8 months ago #

    Try re-saving your permalinks

  3. SchuminWeb
    Member
    Posted 8 months ago #

    Done. Made no difference, unfortunately.

  4. SchuminWeb
    Member
    Posted 8 months ago #

    I also tried this in my functions.php that I found in a support post from three years ago:

    add_filter('pre_get_posts', 'query_post_type');
    function query_post_type($query) {
      if(is_category() || is_tag()) {
        $post_type = get_query_var('post_type');
    	if($post_type)
    	    $post_type = $post_type;
    	else
    	    $post_type = array('post','article');
        $query->set('post_type',$post_type);
    	return $query;
        }
    }

    This code made the custom post types show in the categories like I wanted, but it caused a lot of PHP errors in the backend - enough to where I pulled it out. Anyone know what's wrong here? Apparently this worked before, but it doesn't anymore.

  5. SchuminWeb
    Member
    Posted 8 months ago #

    Figured it out.

  6. Tschapa
    Member
    Posted 8 months ago #

    can you post your solution, please?

  7. ChecMark
    Member
    Posted 8 months ago #

    I've spent hours and hours on this and can't figure out what I'm doing wrong. My category page - http://www.garycjones.ca/?page_id=2304 comes up perfectly but when I click to see the full post all I get is a blank page. I have made the changes to single.php to call up the single-(category).php file and have created a category.(category).php template and reviewed all of the code, but can't solve this issue.

    Appreciate any help.

    Thanks!

  8. aclight
    Member
    Posted 5 months ago #

    So, it seems that by default, the category page only displays posts with that category. In order to enable it for all post types, you have to set a wp_query before the loop.

    Here is what I used on my category.php template: http://pastebin.com/6bNqc2Cg

    I figured out the answer after googling a bit more, but thought it would be nice if someone else found this when searching through this thread. :)

  9. letheos
    Member
    Posted 5 months ago #

    To make custom post types to show up in a regular category archive you have to add to the query your post type.
    For example try this:
    http://www.site.com/category/category_name/?post_type=my_post_type

    You can use a pre_get_posts filter to add the required post type information. Read this for details:
    http://premium.wpmudev.org/blog/add-custom-post-types-to-tags-and-categories-in-wordpress/

    Shouldn't WP natively support custom post types in category archives?

Reply

You must log in to post.

About this Topic