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();
}
Indeed, you need to add the rewrite tags. They indicate what can come in the place of your
%cal_year%
and similar tags: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
to2010-12-31
or however you saved your custom value. Maybe it just defaults back to the slug?