0

I've once got help with the following function:

function get_event_list( $latest = true, $order = 'ASC', $return = false ) {

    $yesterday = time() - 24*60*60;
    $compare = $latest ? '>' : '<';
    $current_year = '';

    $args = array(
        'post_type' => 'wr_event',
        //'posts_per_page' => is_archive() ? 16 : -1,
        'posts_per_page' => -1,
        'meta_key' => 'event_date',
        'orderby' => 'meta_value_num',
        'order' => $order,
        'meta_value' => $yesterday,
        'meta_compare' => $compare,
        /*'tax_query' => array(
            array(
                'taxonomy'  => 'event_type',
                'field'     => 'slug',
                'terms'     => 'empfehlungen',
                'operator'  => 'NOT IN'
                ),
           )*/
    );

    $loop = new WP_Query( $args );

    // if function should not return any posts
    if ( $loop->have_posts() && $return ) return true;

    echo '<ul class="event-items">';
    while ( $loop->have_posts() ) : $loop->the_post();
        global $post;
        $this_year = get_post_meta( $post->ID, 'event_date', true );
        $this_year = date('Y', (int)$this_year);

        if ( !$latest && ( $this_year != $current_year ) ) :
            if ($current_year != '') echo '</ul></li>';
            echo '<li class="year y'.$this_year.'"><div class="wrapper year-count"><h2>' . $this_year . '</h2><a class="load-year" href="#">Archiv anzeigen</a></div><ul>';
            $current_year = $this_year;
        endif;
            get_template_part( 'inc/event', 'item' );
    endwhile;
    if ($current_year != '') echo '</ul></li>';
    wp_reset_postdata();
    echo '</ul>';
}

The Problem I'm having is, that this function is getting me all events that are older than the current year.

How can I transform this function in order to add a param that get's me whatever year I pass in?

The goal I'm after is that I don't want to load all events at once, but rather when clicking on "2011" I want to retrieve those events via ajax.

Can anyone help me out here please?

Thank you in advance, Matt

2

I've modified above function somewhat

function get_event_list( $latest = true, $order = 'ASC', $return = false, $year ) {
    // Pass year i.e $year
    $enddate = strtotime($year."-12-31"); // year's last date
    $startdate = strtotime( ($year-1)."-12-31"); year's first date

$yesterday = array($startdate, $enddate ); // Fetch posts between these dates
$compare = 'BETWEEN';// Between above two dates
$current_year = '';

$args = array(
    'post_type' => 'wr_event',
    //'posts_per_page' => is_archive() ? 16 : -1,
    'posts_per_page' => -1,
    'meta_key' => 'event_date',
    'orderby' => 'meta_value_num',
    'order' => $order,
    'meta_value' => $yesterday,
    'meta_compare' => $compare,
    /*'tax_query' => array(
        array(
            'taxonomy'  => 'event_type',
            'field'     => 'slug',
            'terms'     => 'empfehlungen',
            'operator'  => 'NOT IN'
            ),
       )*/
);

$loop = new WP_Query( $args );

Its untested but should work. Let me know if it worked or not

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.