1

I'm trying to rewrite my permalinks using meta data from my post. The permalink rewrite works- but I'm getting the 404 issue when I try to view the post.

I've flushed by visiting the permalink settings page, and still no dice.

Could someone offer a clue as to why this wouldn't work?

add_filter('post_type_link','calendar_link_filter',1,3);

function calendar_link_filter( $post_link, $id = 0, $leavename = FALSE ) {
    $post = get_post($id);
    if($post->post_type != 'super_duper') {
            return $post_link;
    }
    $date = get_post_meta($post->ID,'event_start_date',true);
    $date = strtotime($date);
    $str = $post_link;
    $str = str_replace('%cal_year%',date("Y",$date),$str);
    $str = str_replace('%cal_month%',date("m",$date),$str);
    $str = str_replace('%cal_day%',date("d",$date),$str);
    return $str;

}


add_action( 'init', 'create_my_post_types' );

function create_my_post_types() {
register_post_type( 'super_duper',
    array(
        'labels' => array(
            'name' => __( 'Super Dupers' ),
            'singular_name' => __( 'Super Duper' )
        ),
        'hierarchical' => false,
        'show_ui' => true, // UI in admin panel
        'publicly_queryable' => true,
        'rewrite' => true,
        'public' => true,
        'rewrite' => array('slug' => 'events8/%cal_year%/%cal_month%/%cal_day%'),


    )
);
flush_rewrite_rules();
}
2
  • I believe it has to do with adding rewrite tags...
    – Joe
    Nov 23, 2010 at 20:27
  • Joe, can you leave a comment it Jan Fabry's answer was helpful?
    – hakre
    Feb 5, 2011 at 17:25

1 Answer 1

1

Indeed, you need to add the rewrite tags. They indicate what can come in the place of your %cal_year% and similar tags:

add_rewrite_tag( '%cal_year%', '([0-9]{4})' );
add_rewrite_tag( '%cal_month%', '([0-9]{1,2})' );
add_rewrite_tag( '%cal_day%', '([0-9]{1,2})' );

This works, but I don't understand how: it seems to me something is missing to let WordPress know how it should get from 2010/12/31 to 2010-12-31 or however you saved your custom value. Maybe it just defaults back to the slug?

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.