Advertisement
Beee

Create yearly archives for races

Jun 17th, 2017
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.89 KB | None | 0 0
  1. // template
  2. $event_args              = array(
  3.     'post_type'       => 'event',
  4.     'type'            => 'yearly',
  5.     'format'          => 'custom',
  6.     'echo'            => 0,
  7.     'show_post_count' => 1,
  8.     'before'          => '--><li>',
  9.     'after'           => '</li><!--',
  10. );
  11. $event_years             = wp_get_archives( $event_args );
  12. $events                  = str_replace( $home_url, $home_url . '/races', $event_years );
  13. $minus_post_type         = str_replace( '?post_type=event', '', $events );
  14. $context[ 'race_years' ] = '<ul class="list-inline list-races"><!--';
  15. $context[ 'race_years' ] .= str_replace( date( 'Y' ) . '/', '', $minus_post_type );
  16. $context[ 'race_years' ] .= '--></ul>';
  17.  
  18. $context[ 'title' ] = post_type_archive_title( '', false );
  19. array_unshift( $templates, 'archive-' . get_post_type() . '.twig' );
  20.  
  21. // alter query - front-end race archive lists
  22. function alter_query_for_races( $query ) {
  23.     if ( ! is_admin() && $query->is_main_query() && is_post_type_archive( 'event' ) ) {
  24.         // @TODO: meta query on event date, not publication date
  25.         $query->set( 'posts_per_page', '25' );
  26.         if ( ! is_year() ) {
  27.             $query->set( 'year', date( 'Y' ) );
  28.         }
  29.  
  30.         // if ( ! is_year() ) {
  31.         //     $query->set( 'meta_query', array(
  32.         //         array(
  33.         //             'key'     => '_event_start_date',
  34.         //             'value'   => array('2017-01-01','2017-12-31'),
  35.         //             'compare' => 'BETWEEN',
  36.         //         ),
  37.         //     ) );
  38.         // }
  39.  
  40.         if ( get_query_var( 'continent' ) ) {
  41.             $query->set( 'meta_query', array(
  42.                 array(
  43.                     'key'   => 'idf_continent_selector',
  44.                     'value' => get_query_var( 'continent' ),
  45.                 ),
  46.             ) );
  47.         }
  48.     }
  49. }
  50. add_action( 'pre_get_posts', 'alter_query_for_races' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement