I have a problem, after upgrading to wordpress 4.0 my custom post type isn't working anymore, here's how I register my custom post type:
function my_post_type() {
$labels = array(
'name' => 'Staff'
);
$args = array(
'labels' => $labels,
'public' => true,
'exclude_from_search' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_nav_menus' => false,
'show_in_menu' => true,
'show_in_admin_bar' => true,
'menu_position' => 20,
'menu_icon' => null,
'capability_type' => 'post',
'hierarchical' => true,
'supports' => array('title','editor','thumbnail','custom-fields','page-attributes'),
'has_archive' => false,
'rewrite' => false,
'query_var' => true,
'can_export' => true
);
register_post_type('my_staff', $args);
}
add_action('init', 'my_post_type');
The problem is that permalink to this post type gives 404, if I set 'hierarchical' to false, everything works, but the thing is that I need 'hierarchical' set to true, any solutions to this?
I ran into this same issue after upgrading to WP 4.0 earlier today--views to single posts of one of my hierarchical custom post types showed 404 after the upgrade.
I did some digging into the WP core to see what change was made that might be causing the problem and discovered this:
https://core.trac.wordpress.org/changeset/28803
Reverting back to the previous code fixed the 404 issue for me. Can't tell if this is an indication of my own poor CPT implementation or an unforeseen bug with this change in Core. That said, if others with this problem would like to try this solution, it's a relatively simple change.
Replace line 2558 in /wp-includes/query.php from this:
if ( ! $ptype_obj->hierarchical ) {
to this:
if ( ! $ptype_obj->hierarchical || strpos($q[ $ptype_obj->query_var ], '/') === false ) {
I had the same issue, and I was able to solve this by setting 'hierarchical' => false in the register_post_type arguments.
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
'has_archive' => true,
'menu_icon' => '',
'show_in_menu' => 'bcs-options',
'rewrite' => array('slug' => 'stories'),
'capability_type' => 'page',
'hierarchical' => false,
'menu_position' => null,
'supports' => array('title','editor','thumbnail','excerpt','comments','revisions')
);
I was still able to set the custom post parent manually in my code and everything works again as before. Not sure why Wordpress 4.0 broke things, but my guess is it has to do with how the new version handles permalinks for custom post types where hierarchical is set to true. Someone with a deeper knowledge of the Wordpress code base might be able to elaborate on this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With