• Hi there,

    Is it possible to rewrite the permalink for a basic post, similair to the way it has been done for a custom-post-type in the following code below?

    I have event postings and do not wish for the permalink to show the post date, rather the actual event date.

    I feel that custom post types are unnecessary for what I need- but would really like to be able to modify my permalinks based on custom meta…

    add_action( 'init', 'create_calendar_type' );
    function create_calendar_type() {
              register_post_type( 'calendar',
                array(
                  'labels' => array(
                    'name' => __( 'Events2' ),
                    'singular_name' => __( 'Event2' )
                  ),
                  'public' => true,
                  'rewrite' => array('slug' => 'events2/%cal_year%/%cal_month%/%cal_day%'),
                  'show_ui' => true,
                  'capability_type' => post,
    
                )
    );
    }
    
    function calendar_link_filter( $post_link, $id = 0, $leavename = FALSE ) {
            $post = get_post($id);
            if($post->post_type != 'calendar') {
                    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_filter('post_type_link','calendar_link_filter',1,3);
  • The topic ‘Rewrite Permalink based on Metadata’ is closed to new replies.