Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Category archive page leads to 404 (related to Custom Post Types) WordPress

  • SOLVED

Hi guys,

I've noticed that my archive page for each category leads to a 404;

../category/sports/ = 404
../category/cooking/ = 404
..etc


I am creating posts via a Custom Post Type and in that Custom Post Type I am assigning posts to their respective category but when visiting the respective category page I receive a 404.

What I have noticed is that if I view /category/uncategorized/ which is provided as per the default WordPress installation I see the default "Hello World!" post, which was not created by my custom post type; hence I believe my issue to be related to the custom post type.


// Custom Post Type

add_action( 'init', 'create_post_type' );

function create_post_type() {
global $wp_rewrite;
register_post_type( 'portfolio',
array(
'labels' => array(
'name' => __( 'Portfolio' ),
'singular_name' => __( 'portfolio' ),

),
'rewrite' => TRUE,
'has_archive' => 'portfolio',
'public' => true,
'menu_position' => 4,
'hierarchical' => true,
'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail' ),
'rewrite' => array( 'slug' => 'portfolio'),
'taxonomies' => array( 'post_tag', 'category'),
'menu_icon' => get_bloginfo('template_directory') . '/images/portfolio.png',
));
$wp_rewrite->flush_rules();
}


I've tried adding and removing various components of the above to no avail; any idea what I might be doing wrong?

Regards,
WP

Answers (4)

2011-11-22

Francisco Javier Carazo Gil answers:

Have you tried to init your category?


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');


In your functions.php you should insert your own post_type, in this case, "portfolio".


Wordpressing comments:

Hi Francisco,

Enter my custom post type where it says 'post_type' or?


Wordpressing comments:

I found this code snippet to work for me;

add_filter('pre_get_posts', 'query_post_type');
function query_post_type($query) {
if(is_category() || is_tag() || is_home() && empty( $query->query_vars['suppress_filters'] ) ) {
$post_type = get_query_var('post_type');
if($post_type)
$post_type = $post_type;
else
$post_type = array('post','portfolio','nav_menu_item');
$query->set('post_type',$post_type);
return $query;
}
}

2011-11-22

Luis Cordova answers:

right the category I think should be under a different url slug because

'rewrite' => array( 'slug' => 'photo-blog'),

so try /photo-blog/


Luis Cordova comments:


<strong>has_archive</strong>
(boolean or string) (optional) Enables post type archives. Will use string as archive slug. Will generate the proper rewrite rules if rewrite is enabled.
Default: false
<strong>rewrite</strong>
(boolean or array) (optional) Rewrite permalinks with this format. False to prevent rewrite.
Default: true and use post type as slug


Wordpressing comments:

Sorry Luis, photo-blog should have read portfolio.

Anyway, if I take out

'rewrite' => array( 'slug' => 'portfolio'),

Should I not be able to access the posts created by this custom post type under;

/category/category_name

?

Because I tried removing the rewrite rule but I still receive a 404.

I need to be able to see posts associated with a category no matter if they are created with regular posts or custom post types.

Because if I build my menu under Appearance/Menus in the dashboard and use categories to do so; this poses a problem by not showing the posts that were created by my custom post type and only those by the regular post edit screen.

2011-11-22

Luis Abarca answers:

You need to add your post type to your query, you also have a duplicated key 'rewrite' in your register_post_type function call.

Like Francisco said:

add_filter('pre_get_posts', 'init_category');

function init_category($wp_query)
{
$vars = $wp_query->query_vars;

if (is_category() && !array_key_exists('portfolio', $vars)) {

$vars = array_merge( $vars, array('post_type' => 'any') );
$wp_query->query_vars = $vars;
} // endif

return $request;
}

add_action( 'init', 'create_post_type' );

function create_post_type() {
global $wp_rewrite;

register_post_type( 'portfolio',
array(
'labels' => array(
'name' => __( 'Portfolio' ),
'singular_name' => __( 'portfolio' ),

),
'has_archive' => true,
'public' => true,
'menu_position' => 4,
'hierarchical' => true,
'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail' ),
'rewrite' => array( 'slug' => 'portfolio'),
'taxonomies' => array( 'post_tag', 'category'),
'menu_icon' => get_bloginfo('template_directory') . '/images/portfolio.png',
));

$wp_rewrite->flush_rules();
}

2011-11-25

Arnav Joy answers:

Hi ,

if you are using archieve.php for this post type only then this code may help you.

use this code in your functions.php
and then reset your permalink

function changept() {

if(!is_admin())
{
if(is_archive())
set_query_var( 'post_type', array( 'post', 'portfolio' ) );
}
return;
}