I have 2 post types in my theme, the default post type and a custom article post type. I publish my posts under different categories, and my articles ONLY under a single category named blog, which doesn’t contain any default post type.

I don’t want my article to be shown next to post in archive and category pages, so i use this custom query in my archive.php to show the results:

//Check if it's a custom post type archive, if it is not, then show the usual archive results
if (!is_post_type_archive('article')){
    if ( have_posts() ) {
        while ( have_posts() ) {
            the_post(); 
            the_title();
        }
    } else { // If there's no post, there is a chance that we are on the BLOG category, so check if any custom posts exist here
        $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
        $article_query = new WP_Query( array(
                'post_type' => 'article',
                'cat' => get_query_var('cat'),
                'paged' => $paged
                )
            );
        if ($article_query->have_posts()) {
            while ($article_query->have_posts()) {
                $article_query->the_post();
                the_title(); 
            }
        } else {
            echo 'No results';
        }
    }
} else { // If it's custom post type archive, show results
    if ( have_posts() ) {
        while ( have_posts() ) { 
            the_post();
            the_title();
        }
    } else {
        echo 'No posts';
    }
} ?>

But this always returns a 404 error, even when there is more than 1 page of posts in BLOG category.

Is it possible to workaround this?
Thanks.

PS : I can’t use if (is_category('blog')) since i don’t know what category my customers will use to publish their articles under.

Read more here: Why my query returns 404 error on page 2 and afterward?


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: