mardi 31 mars 2015

When I add custom post type permalink rewrite, my regular post permalinks stop working. Can't get both to work at the same time

really struggling with this one, so any help would be much appreciated. My site has both regular post posts, and a custom post type called "articles."


I'm trying to get it to work so that my regular posts will use the /%category%/postname%/ permalink structure, (which I have set up in settings). This is working fine, until I add a custom rewrite for my article post type. I'd like articles to follow a /%issue%/%postname%/ structure. I can get this working great with the following:



add_filter( 'post_type_link', 'wpa_show_permalinks', 1, 2 );

function wpa_show_permalinks( $post_link, $id = 0 ){
$post = get_post($id);
if ( is_object( $post ) && $post->post_type == 'article' ){
$terms = wp_get_object_terms( $post->ID, 'issue_tax' );
if( $terms ){
return str_replace( '%issue%' , $terms[0]->slug , $post_link );
}
}
return $post_link;
}


where my post type is registered like this:



function article_post_type() {

$labels = array(
'name' => _x( 'Magazine Articles', 'Post Type General Name', 'text_domain' ),
'singular_name' => _x( 'Magazine Article', 'Post Type Singular Name', 'text_domain' ),
'menu_name' => __( 'Magazine Articles', 'text_domain' ),
'name_admin_bar' => __( 'Magazine Articles', 'text_domain' ),
'parent_item_colon' => __( 'Parent Article:', 'text_domain' ),
'all_items' => __( 'All Articles', 'text_domain' ),
'add_new_item' => __( 'Add New Article', 'text_domain' ),
'add_new' => __( 'Add New', 'text_domain' ),
'new_item' => __( 'New Article', 'text_domain' ),
'edit_item' => __( 'Edit Article', 'text_domain' ),
'update_item' => __( 'Update Article', 'text_domain' ),
'view_item' => __( 'View Article', 'text_domain' ),
'search_items' => __( 'Search Article', 'text_domain' ),
'not_found' => __( 'Not found', 'text_domain' ),
'not_found_in_trash' => __( 'Not found in Trash', 'text_domain' ),
);
$rewrite = array(
'slug' => '%issue%',
'with_front' => false,
'pages' => true,
'feeds' => true,
);
$args = array(
'label' => __( 'article', 'text_domain' ),
'description' => __( 'Magazine Articles and Features', 'text_domain' ),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'trackbacks', 'revisions', 'custom-fields', 'page-attributes', ),
'taxonomies' => array( 'issue_tax', 'category', 'featured_media', 'tag' ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'menu_icon' => 'dashicons-welcome-write-blog',
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'rewrite' => $rewrite,
'capability_type' => 'page',
);
register_post_type( 'article', $args );

}


add_action( 'init', 'article_post_type', 0 );


I add that, reset permalink settings, and the article permalinks start working as indended - BUT - as soon as I get that working, my regular posts start displaying a 404.


Why am I unable to get both to work at the same time? Am I missing a piece somewhere?


Thanks for any advice!


-erin


Aucun commentaire:

Enregistrer un commentaire