How can I make my custom posts appear in their assigned category url?

I am trying to solve this problem:

I created new custom post types. When I create a post and assign a category (lets say category-1) to it, and when I try to visit the category-1 url, it appears “There has been an error.”

Read More

I tried solving the issue based on the Michal Mau post (How to display regular posts & custom post types that fall under a category using just the generic category template?).

My edited code after adding Michal Mau suggestion looks like this:

<?php if (have_posts()) :
global $wp_query;
$args = array_merge( $wp_query->query, array( 'post_type' => 'any' ) );
query_posts( $args );

while (have_posts()) : the_post(); ?>
  <article id="post-<?php the_ID(); ?>" <?php post_class('post-holder'); ?>>
    <?php if(has_post_thumbnail()) {
                echo '<figure class="featured-thumbnail"><span class="img-wrap"><a href="'; the_permalink(); echo '">';
                echo the_post_thumbnail();
                echo '</a></span></figure>';
                }
            ?>

The problem wasn’t solved. I don’t know if there is a mistake pasting the code or any other mistake.

Related posts

Leave a Reply

1 comment

  1. The query_posts solution is not reccomended (see you don’t know query). You should use pre_get_posts hook instead. See:

    function my_extend_category_by_custom_post_type( $query ) {
        if ( $query->is_category() && $query->is_main_query() ) {
            $query->set( 'post_type', 'any' );
        }
    }
    add_action( 'pre_get_posts', 'my_extend_category_by_custom_post_type' );
    

    PS: this should go into your functions.php if (custom post type is set from there) or, preferably, from site specific plugin.