Using the below hooks, I tried to rewrite the permalinks of my custom post type magazine_issue to be made of values of additional meta-fields I added to the edit screen. The problem with my actual code is that it seems to register URLs of all found custom posts in the desired way but unfortunately, when requesting those pages, it will only return the latest generic post ("Hello World") troughout. How can I fix this?

function magazine_issues_rewrite() {
  global $wp_rewrite;
  $queryarg = 'post_type=magazine_issue&p=';
  $wp_rewrite->add_rewrite_tag('%issuenumber%', '([^/]+)', $queryarg);
  $wp_rewrite->add_rewrite_tag('%issueyear%', '([^/]+)', $queryarg);
  $wp_rewrite->add_permastruct('issue_url_by_meta', '/'.__('issues', 'doev').'/%issueyear%/%issuenumber%/', false);
add_action('init', 'magazine_issues_rewrite');

function magazine_issues_permalink($post_link, $id = 0, $leavename) {
    global $wp_rewrite;
    $post = &get_post($id);
    if (is_wp_error($post)) return $post;

    if ('magazine_issue'==get_post_type($post)) {
        $newlink = $wp_rewrite->get_extra_permastruct('issue_url_by_meta');
        $newlink = str_replace("%issuenumber%", get_post_meta($post->ID,'issuenumber',true), $newlink); 
        $newlink = str_replace("%issueyear%", get_the_date('Y'), $newlink);
        $newlink = home_url(user_trailingslashit($newlink));
        return $newlink;
add_filter('post_type_link', 'magazine_issues_permalink', 1, 3);

1 Answer 1


It's a bit simpler to let WordPress handle generating the permastruct when you register your post type. I also recommend against using global variables, there are API functions for most of what you need to accomplish with the rewrite class.

function magazine_issues_rewrite() {

    add_rewrite_tag( '%issuenumber%', '([^&]+)' );
    add_rewrite_tag( '%issueyear%', '([^&]+)' );

    // post type registration, other args removed for clarity
    $args = array(
        'rewrite' => array( 'slug' => 'issue/%issueyear%/%issuenumber%' ),
    register_post_type( 'issue', $args );

add_action( 'init', 'magazine_issues_rewrite' );

I'm not sure whether or not that will fix your problem, I haven't tested your full code.

  • Thanks for your help! This seemed to be a compact solution, but unfortunately it results in a 400. Commented Jan 21, 2014 at 7:57
  • Sorry, I was to slow to edit last comment... Thanks for your help! Initially, this looks like a compact solution, but it will not do the kind of rewriting I'm trying to achieve. It doesn't replace those words within the URL by my meta data. Isn't there something left which would define, that I want to fetch the meta-data? Something like ´get_post_meta($post->ID,'issuenumber',true)´ ? Commented Jan 21, 2014 at 8:10
  • You still need your post_type_link filter to generate the Urls, but that doesn't have anything to do with rewrites or 404 problems, as long as they are in the correct format for your rewrite rules to parse
    – Milo
    Commented Jan 21, 2014 at 15:24

Your Answer

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

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