1

currently building a custom magazine site and running into some rewrite issues.

I'm using the default Posts post type, and have added in a custom Taxonomy for Issues (issue_cats). Everything works great for the most part but I'm struggling to get the permalinks structured correctly. I'm also using the default "Categories" as well for general categories for the articles.

In case it matters, I also have a page with a slug 'issues' that is simply a page with custom page template displaying a custom layout of terms in the issue_cats taxonomy.

The link structure to view all Issues is: site.com/issues/

The link structure looks like this for individual Issues: site.com/issues/issue-name and works like a treat.

The problem is with the single posts. The URLs ideally should be: site.com/issues/issue-name/post-name.

Have tried a couple of things and in some cases got the single posts to work/have the correct slug, but it causes all regular pages on the site to return 404's. Googled for days, and thought I found a solution at one point, but it doesn't work as of 4.5.2 apparently. Any thoughts?

Here is my custom Issue Taxonomy code:

function zbt_register_custom_tax() {
    $labels = array(
        'name'              => 'Issue',
        'singular_name'     => 'Issue',
        'search_items'      => 'Search Issues',
        'all_items'         => 'All Issues',
        'parent_item'       => 'Parent Issue',
        'parent_item_colon' => 'Parent Issue',
        'edit_item'         => 'Edit Issue',
        'update_item'       => 'Update Issue',
        'add_new_item'      => 'Add Issue',
        'new_item_name'     => 'New Issue',
        'menu_name'         => 'Issues'
    );
    $args = array(
        'labels' => $labels,
        'show_admin_column' => true,
        'hierarchical' => true,
        'rewrite' => array( 'slug' => 'issues', 'with_front' => false ),
        'show_ui' => true,
        'query_var' => true,
        'public' => true,
    );
    register_taxonomy( 'issue_cats', 'post', $args );
}
add_action( 'init', 'zbt_register_custom_tax', 99 );
5
  • What happens if you put that structure into your site admin Permalinks options page? Aug 12, 2016 at 17:19
  • Since the issue-name is a custom taxonomy, there is no way to put that structure into the base permalink options.
    – 61Pixels
    Aug 12, 2016 at 17:24
  • There may be if you make a custom permastruct. Before looking into it I wondered if it works if you test with a known term. Aug 12, 2016 at 17:26
  • Thanks Andy, are you saying take a known term like "Issue-5" and putting it directly in the permalink custom rewrite? like /issues/issue-5/%post-name%/ ? I'm not sure how that would work, but am happy to try it in a few. I actually think I might have just cracked the beast though. Update to come.
    – 61Pixels
    Aug 12, 2016 at 17:31
  • Yeh that's it. Just as a quick and crude test that the approach may work. I might look out for your own answer first though, if you think you've worked it out! Aug 12, 2016 at 17:37

1 Answer 1

0

This is what we ended up coming up with that seems to work. I'm not sure if it's the best or right approach but it seems to do the trick. Anyone forsee any problems with this route?

//Re-register default post type for rewrite purposes.
function zbt_reregister_post() {
    register_post_type( 'post', array(
        'labels' => array(
            'name_admin_bar' => _x( 'Article', 'add new on admin bar' ),
        ),
        'public'  => true,
        '_builtin' => false,
        '_edit_link' => 'post.php?post=%d',
        'capability_type' => 'post',
        'map_meta_cap' => true,
        'hierarchical' => false,
        'rewrite' => array( 'slug' => '/issues/%issue_cats%', 'with_front' => false ),
        'query_var' => false,
        'supports' => array( 'title', 'editor', 'author', 'excerpt', 'comments', 'revisions'),
    ) );
}
add_action( 'init', 'zbt_reregister_post', 99 );

// Rewrite default post permalinks to add issue (e.g. /summer-2016/post-name/).
function zbt_add_issue_to_permalinks( $post_link, $post ){
    if ( is_object( $post ) && $post->post_type == 'post' ){
        $terms = wp_get_object_terms( $post->ID, 'issue_cats' );
        if( $terms ){
            return str_replace( '%issue_cats%' , $terms[0]->slug , $post_link );
        }
    }
    return $post_link;
}
add_filter( 'post_type_link', 'zbt_add_issue_to_permalinks', 1, 2 );
3
  • Looks cool at first glance. I guess there's something about reregistering a built in type that makes me uneasy. Does it work if you use %issue_cats% in the Permalink Options in admin instead? Aug 13, 2016 at 12:55
  • Yeah I have the same reserves.. I just commmented out the reregistering of the post and updated the permalinks to a custom one accordingly... When I click save, it takes me to a WordPress failure page with just a message saying "Are you sure you want to do this?" with no button or anything. So I'm guessing no, it won't work that way :)
    – 61Pixels
    Aug 13, 2016 at 14:49
  • Not sure what I screwed up, but after a friend confirmed it worked for him, tried again and yeah, it's working now after a minor adjustment. Thanks for your input!
    – 61Pixels
    Aug 13, 2016 at 21:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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