WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Custom Post Types + Paginated Category Archive = 404 (3 posts)

  1. Mike
    Member
    Posted 4 years ago #

    Hello,

    I've been struggling with this problem a while now so I'm giving in and asking for assistance.

    So, I have a custom post type called 'product' - this custom post type is set up to use the 'category' taxonomy.

    The first page of the category archive works fine - I have placed:

    global $wp_query;
    	query_posts(
    	array_merge(
    		array('post_type' => 'product'),
    		$wp_query->query
    	)
    );

    at the top of category.php in the template. When you go to page 2 however, it gives a 404.

    From what I can tell this is because the query functions/template loader are making it a 404 before the post_type rule is seen because its not finding any 'posts' on page 2 of the archive.

    I've tried adding hooks into functions.php to try to get the post_type recognised earlier with no success.

    Any ideas?

  2. Mike
    Member
    Posted 4 years ago #

    For anyone else, this is how I had to fix it:

    function init_category($request) {
    	$vars = $request->query_vars;
    	if (is_category() && !is_category('Blog') && !array_key_exists('post_type', $vars)) :
    		$vars = array_merge(
    			$vars,
    			array('post_type' => 'any')
    		);
    		$request->query_vars = $vars;
    	endif;
    	return $request;
    }
    add_filter('pre_get_posts', 'init_category');
  3. Robert
    Member
    Posted 3 years ago #

    Thanks a lot! Been puling out my hair for a couple of hours now...

Topic Closed

This topic has been closed to new replies.

About this Topic