WordPress.org

Ready to get started?Download WordPress

Forums

WordPress Calendar For Custom Post Types
[resolved] Showing posts from calendar (7 posts)

  1. Woyto
    Member
    Posted 2 years ago #

    Hello,
    Currently, on calendar widget a day on which post was added is highlighted but pressing on it you get 404 error. Is it possible to go to all posts (from selected taxonomies) which were published on that day?

    http://wordpress.org/extend/plugins/post-types-calendar/

  2. Stas Su?cov
    Member
    Plugin Author

    Posted 2 years ago #

    Yes, it's a limitation caused by WordPress lack of dynamic archive pages.
    You can use something like this to link to latest article post instead of broken archive page.

    /**
    * Find an event and replace the calendar day link
    */
    function cptc_calendar_day_link( $url, $year, $month, $day ) {
    # Filter post types here
    #if ( !is_post_type_archive( 'event' ) )
    # return $url;
    
    $day_event = get_posts( array(
        'numberposts' => 1,
        'post_type' => 'event',
        'day' => $day,
        'monthnum' => $month,
        'year' => $year
    ) );
    
    $day_event_url = get_post_permalink( reset( $day_event ) );
    if ( $day_event_url )
        return $day_event_url;
    return $url;
    }
    add_filter( 'day_link', 'cptc_calendar_day_link', 10, 4 );
    
    /**
    * Find an event and replace the calendar month link
    */
    function cptc_calendar_month_link( $url, $year, $month ) {
    return add_query_arg( 'm', $year . zeroise( $month, 2 ), get_post_type_archive_link( 'event' ) );
    }
    add_filter( 'month_link', 'cptc_calendar_month_link', 10, 3 );

    Good luck.

  3. Cristiano
    Member
    Posted 2 years ago #

    Yes, it works! Thanks!

  4. Cristiano
    Member
    Posted 2 years ago #

    Ops... it works with the days, but it don´t with the month links!!!

    Could you take a look on that??
    Thanks!

  5. Cristiano
    Member
    Posted 2 years ago #

    Sorry, it was my fault. I didn´t change the custom post type name in your script before. Everything is working fine now!

    Thanks!!!

  6. Cristiano
    Member
    Posted 2 years ago #

    Hi Stas,

    I still have a problem with that... in case I have more than one event at the same day, the link should appoint to a day archive, right?

    Is that a way to do that?
    Thanks!

  7. Cristiano
    Member
    Posted 2 years ago #

    I solve it this way:

    1) Install the Custom Post Type Archives plugin, that creates date post type archives.

    2) Change your filter, to count posts and change day links when it has more than one event/post:

    /**
    * Find an event and replace the calendar day link
    */
    function cptc_calendar_day_link( $url, $year, $month, $day ) {
    # Filter post types here
    #if ( !is_post_type_archive( 'events' ) )
    # return $url;
    
    $day_event = get_posts( array(
        'post_type' => 'events',
        'day' => $day,
        'monthnum' => $month,
        'year' => $year
    ) );
    
    $myposts = count($day_event);
    
    if ($myposts == 1) {
    	$day_event_url = get_post_permalink( reset( $day_event ) );
    } else {
    	$day_event_url = site_url() . "/events/" . $year . "/" . $month . "/" . $day;
    }
    
    if ( $day_event_url )
        return $day_event_url;
    return $url;
    }
    add_filter( 'day_link', 'cptc_calendar_day_link', 10, 4 );
    
    /**
    * Find an event and replace the calendar month link
    */
    function cptc_calendar_month_link( $url, $year, $month ) {
    return add_query_arg( 'm', $year . zeroise( $month, 2 ), get_post_type_archive_link( 'events' ) );
    }
    add_filter( 'month_link', 'cptc_calendar_month_link', 10, 3 );

    That is it. Thanks.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic