WordPress.org

Ready to get started?Download WordPress

Forums

register_post_type and rewriting issue (1 post)

  1. o3strategies
    Member
    Posted 3 years ago #

    I'm attempting something that I've gotten to half work.

    I have a custom post type registered as calendar. This post type is essentially an event. I have a custom meta box for the event date, event start time and event end time as well as some other ancillary information.

    What I want to do is have a permalink to this event in this format:

    /events/year/month/day/post-name

    The year, month, and day variables are NOT the publish year, month, and day, but rather, the values stored in wp_postmeta. I've gotten my permalink to format correctly using the meta values, but I get a 404 when visiting. I believe this is because I have not registered the rewrite.

    Here's the code:

    add_action( 'init', 'create_calendar_type' );
    	function create_calendar_type() {
    	  register_post_type( 'calendar',
    	    array(
    	      'labels' => array(
    	        'name' => __( 'Events' ),
    	        'singular_name' => __( 'Event' )
    	      ),
    	      'public' => true,
    	      'rewrite' => array('slug' => 'events'),
    	      '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);

    When I create an event, the permalink correctly formats under the post title upon publish and upon entering a value for the event date.

    I get a 404 after that.

    I'm wondering what I need to do to register the rewrite.

    Any clues into this?

Topic Closed

This topic has been closed to new replies.

About this Topic