Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Other WordPress: register_post_type and custom permalink

  • This topic is empty.
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #30038
    o3strategies
    Member

    Quick question for the wordpress gurus out there.

    I have created a new post type using register_post_type of calendar. Basically, it works like a post but I’ve added some meta boxes that deal exclusively with an event details (namely, an event date).

    The URL of the event comes in as /events/slug. Not bad, but the company I building for will have several events named the same thing, so I want to avoid /events/slug-2, /events/slug-3, etc.

    So I want to tie the permalink to the meta data for the post and format the permalink like this /events/2010/08/25/slug. This will resolve my issue. The date in that permalink IS NOT the publish date of the post, rather, the post date meta data that I created.

    I’ve gotten as far as to get the permalinks actually formatting correctly. However, I get a 404 when visiting because, I presume, I have not registered the rewrite rule for that specific instance. I’ve googled all over God’s green Internet and cannot get this to work.

    Here’s the two functions that creates the post type and formats the permalink.


    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/%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);

    Any clues would be GREATLY appreciated.

    #81384
    o3strategies
    Member

    Thanks for the input. Yes, I’ve tried that. I also think that visiting the Admin->Permalinks page also flushes the rules.

    But to no avail. I think the issue is that my permalink structure is /year/month/slug and I’m trying to create a whole different structure. Therefore, WordPress is looking at the URL and doesn’t know what to do with it.

    #77848
    jaredwilli
    Member

    It’s a known bug. Happens cuz there’s no archives template for custom post types. It will be fixed in 3.1 and this will not be a problem anymore (hopefully).

Viewing 3 posts - 1 through 3 (of 3 total)
  • The forum ‘Other’ is closed to new topics and replies.