WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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();
}
share|improve this question
    
I believe it has to do with adding rewrite tags... – Joe Nov 23 '10 at 20:27
    
Joe, can you leave a comment it Jan Fabry's answer was helpful? – hakre Feb 5 '11 at 17:25

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?

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.