0

I have created a custom post type portfolio and custom categories. Everything is working fine as expected however when I click into the custom post types categories and click the view button for a given category the page that comes up isn't listing the results eror 404. For example, if the post type is "portfolio" and the category is "web-design" When I click the view button for this category I would suspect to see a page with all the web-design post...

Not sure what I am doing wrong here.

Code generate with http://generatewp.com/

 // Register Custom Post Type

function portfolio_post_type() {

$labels = array(
    'name'                => _x( 'Portfolios', 'Post Type General Name', 'owd' ),
    'singular_name'       => _x( 'Portfolio', 'Post Type Singular Name', 'owd' ),
    'menu_name'           => __( 'Portfolio', 'owd' ),
    'parent_item_colon'   => __( 'Parent Portfolio:', 'owd' ),
    'all_items'           => __( 'All Portfolios', 'owd' ),
    'view_item'           => __( 'View Portfolio', 'owd' ),
    'add_new_item'        => __( 'Add New Portfolio', 'owd' ),
    'add_new'             => __( 'New Portfolio', 'owd' ),
    'edit_item'           => __( 'Edit Portfolio', 'owd' ),
    'update_item'         => __( 'Update Portfolio', 'owd' ),
    'search_items'        => __( 'Search portfolios', 'owd' ),
    'not_found'           => __( 'No portfolios found', 'owd' ),
    'not_found_in_trash'  => __( 'No portfolios found in Trash', 'owd' ),
);
$args = array(
    'label'               => __( 'portfolio', 'owd' ),
    'description'         => __( 'Portfolio information pages', 'owd' ),
    'labels'              => $labels,
    'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'custom-fields', ),
    'hierarchical'        => true,
    'public'              => true,
    'show_ui'             => true,
    'show_in_menu'        => true,
    'show_in_nav_menus'   => true,
    'show_in_admin_bar'   => true,
    'menu_position'       => 5,
    'menu_icon'           => get_stylesheet_directory_uri() . '/images/portfolio.png',
    'can_export'          => true,
    'has_archive'         => true,
    'exclude_from_search' => false,
    'publicly_queryable'  => true,
    'capability_type'     => 'page',
);
register_post_type( 'portfolio', $args );
flush_rewrite_rules();
// http://dreamdare.org/tips-tricks/how-to-fix-wordpress-custom-post-type-permalink-404-error/

} add_action( 'init', 'portfolio_post_type', 0 );

// Register Custom Taxonomy function custom_taxonomy_portfolio() {

$labels = array(
    'name'                       => _x( 'Portfolio Categories', 'Taxonomy General Name', 'owd' ),
    'singular_name'              => _x( 'Portfolio Category', 'Taxonomy Singular Name', 'owd' ),
    'menu_name'                  => __( 'Portfolio Category', 'owd' ),
    'all_items'                  => __( 'All Portfolio Category', 'owd' ),
    'parent_item'                => __( 'Portfolio Category', 'owd' ),
    'parent_item_colon'          => __( 'Parent Portfolio Category:', 'owd' ),
    'new_item_name'              => __( 'New Portfolio Category', 'owd' ),
    'add_new_item'               => __( 'Add Portfolio Category', 'owd' ),
    'edit_item'                  => __( 'Edit Portfolio Category', 'owd' ),
    'update_item'                => __( 'Update Portfolio Category', 'owd' ),
    'separate_items_with_commas' => __( 'Separate portfolio categories with commas', 'owd' ),
    'search_items'               => __( 'Search Portfolio Category', 'owd' ),
    'add_or_remove_items'        => __( 'Add or remove Portfolio Category', 'owd' ),
    'choose_from_most_used'      => __( 'Choose from the most used portfolio category', 'owd' ),
    'not_found'                  => __( 'Not Found Portfolio Category', 'owd' ),
);
$rewrite = array(
    'slug'                       => 'portfolio',
    'with_front'                 => false,
    'hierarchical'               => true,
);
$capabilities = array(
    'manage_terms'               => 'manage_categories',
    'edit_terms'                 => 'manage_categories',
    'delete_terms'               => 'manage_categories',
    'assign_terms'               => 'edit_posts',
);
$args = array(
    'labels'                     => $labels,
    'hierarchical'               => true,
    'public'                     => true,
    'show_ui'                    => true,
    'show_admin_column'          => true,
    'show_in_nav_menus'          => true,
    'show_tagcloud'              => true,
    'rewrite'                    => $rewrite,
    'capabilities'               => $capabilities,
);
register_taxonomy( 'portfolio_category', array( 'portfolio' ), $args );

} // Hook into the 'init' action add_action( 'init', 'custom_taxonomy_portfolio', 0 );

I have archive-portfolio.php and taxonomy-portfolio_category.php

  • You are flushing the rewrite rules too early or too late. Try to use a better approach. – fuxia Feb 28 '14 at 11:38
0

There is a conflict with the taxonomy slug and custom-post-type slug. You are keeping both same.

There are two options: 1) Either remove the rewrite => $rewrite from the taxonomy array and make it like this

$labels = array(
    'name'                       => _x( 'Portfolio Categories', 'Taxonomy General Name', 'owd' ),
    'singular_name'              => _x( 'Portfolio Category', 'Taxonomy Singular Name', 'owd' ),
    'menu_name'                  => __( 'Portfolio Category', 'owd' ),
    'all_items'                  => __( 'All Portfolio Category', 'owd' ),
    'parent_item'                => __( 'Portfolio Category', 'owd' ),
    'parent_item_colon'          => __( 'Parent Portfolio Category:', 'owd' ),
    'new_item_name'              => __( 'New Portfolio Category', 'owd' ),
    'add_new_item'               => __( 'Add Portfolio Category', 'owd' ),
    'edit_item'                  => __( 'Edit Portfolio Category', 'owd' ),
    'update_item'                => __( 'Update Portfolio Category', 'owd' ),
    'separate_items_with_commas' => __( 'Separate portfolio categories with commas', 'owd' ),
    'search_items'               => __( 'Search Portfolio Category', 'owd' ),
    'add_or_remove_items'        => __( 'Add or remove Portfolio Category', 'owd' ),
    'choose_from_most_used'      => __( 'Choose from the most used portfolio category', 'owd' ),
    'not_found'                  => __( 'Not Found Portfolio Category', 'owd' ),
);

$capabilities = array(
    'manage_terms'               => 'manage_categories',
    'edit_terms'                 => 'manage_categories',
    'delete_terms'               => 'manage_categories',
    'assign_terms'               => 'edit_posts',
);
$args = array(
    'labels'                     => $labels,
    'hierarchical'               => true,
    'public'                     => true,
    'show_ui'                    => true,
    'show_admin_column'          => true,
    'show_in_nav_menus'          => true,
    'show_tagcloud'              => true,
              //Removed rewrite from here
    'capabilities'               => $capabilities,
);
register_taxonomy( 'portfolio_category', array( 'portfolio' ), $args );

or 2) rename your taxonomy slug to something else but not portfolio.

$rewrite = array(
    'slug'                       => 'my_portfolio', //Changed from portfolio to my_portfolio
    'with_front'                 => false,
    'hierarchical'               => true,
);

Also remove flush_rewrite_rules(); from your code. And after updating the code just visit settings > permalinks which will save all your settings. Why call an unnecessary function again and again whenever functions.php is called. On the other hand it's not being called at the right place. It's better to remove and visit settings>permalinks page whenever your edit custom post type.

  • Thank you very much, i have no words to thank you, I suspected there was something wrong with the code but do not know what ... now everything works perfectly. Here is the website is a wordpress theme I'm working on what is seen is the test portfolio. wpteme.info/portofolio-big-column wpteme.info – Bucur Ion Feb 28 '14 at 13:26
  • Glad it worked for you. Please accept the answer by clicking check on the left side of answer. – Sudeep K Rana Feb 28 '14 at 14:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.