Reputation: 107
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
Just a follow up to my question - perhaps the thing that I'm really struggling with is why is the post_type_filter function affecting more than just the article post type that I have specified?
Thanks, Erin
Ok, one more super strange thing. This all works if I pass a query parameter at the end of my custom post links, so this works: http://www.mysitename.com/spring-2015/test-article-here/?post_type=article but this gives me a 404 http://www.mysitename.com/spring-2015/test-article-here/
Why would that be? I'm sorry for so many questions, just really trying to get to the bottom of this..!
Thanks again, Erin
Upvotes: 3
Views: 3028
Reputation: 11
From the answer above:
Replace: Replace 'slug' => '%issue%',
with 'slug' => 'article/%issue%',
That didn't work fully for me, so I had to change my post_type_link filter as well.
All I had to do was to change following:
return str_replace( '%issue%' , $terms[0]->slug , $post_link );
With:
return str_replace( 'article/%issue%' , $terms[0]->slug , $post_link );
It's a bit strange and I spent several hours figuring this out. Hope it helps someone in the future!
Upvotes: 0
Reputation: 1
Replace 'slug' => '%issue%',
with 'slug' => 'article/%issue%',
It will definitely work, I have tested it. Kindly note that "article" is the post type name.
Also Save permalinks after making above change.
Upvotes: 0