0

I have a custom-post-type for events and use the normal archive.php template to list all previous events.

The thing is that I use a custom query for my event-post-type inside archive.php like this …

<?php
            if ( is_post_type_archive('wr_event') ) :
                 get_event_list( false, 'DSC' );
            else:
                rewind_posts();
                get_template_part( 'inc/posts' );
            endif;
        ?>

The function get_event_list()

looks like this …

function get_event_list( $latest = true, $order = 'ASC' ) {
    echo '<ul class="event-items">';

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

    $args = array(
        'post_type' => 'wr_event',
        'posts_per_page' => 'posts_per_page' => is_archive() ? 16 : -1,
        'meta_key' => 'event_date',
        'orderby' => 'meta_value_num',
        'order' => $order,
        'meta_value' => $yesterday,
        'meta_compare' => $compare
    );

    $loop = new WP_Query( $args );
    while ( $loop->have_posts() ) : $loop->the_post();
        get_template_part( 'inc/event', 'item' );
    endwhile;
    wp_reset_postdata();
    echo '</ul>';
}

I'm using the same function get_event_list() in my index.php to list all events - and on my index I want to list ALL of them. That's why I have posts_per_page => -1.

In my archives.php where I want to use the same template I want however only 16 "events" to be listed and a pagination underneath.

I have the following function in my functions.php that does work fine in my search-template. But it doesn't for this custom-archive.

function get_pagination_links( $type = 'plain', $endsize = 1, $midsize = 1 ) {
    global $wp_query, $wp_rewrite;  
    $wp_query->query_vars['paged'] > 1 ? $current = $wp_query->query_vars['paged'] : $current = 1;

    // Sanitize input argument values
    if ( ! in_array( $type, array( 'plain', 'list', 'array' ) ) ) $type = 'plain';
    $endsize = (int) $endsize;
    $midsize = (int) $midsize;

    // Setup argument array for paginate_links()
    $pagination = array(
        'base'          => @add_query_arg('paged','%#%'),
        'total'         => $wp_query->max_num_pages,
        'current'       => $current,
        'show_all'      => false,
        'end_size'      => $endsize,
        'mid_size'      => $midsize,
        'type'          => $type,
        'prev_next'     => false
    );

    return paginate_links( $pagination );
}

The pagination shows up underneath my events and only 16 events are listed. However, if I click on page two, the same list of the first 16 events is listed.

Any ideas how to make that pagination work with my custom-post-type archive?

2 Answers 2

2
+25

Here's an analogy to help you out:

You have a book, there are 500 words per page and you open the book at a page and read. You then hand then close the book and hand it to me, asking me to read the next page. Which page?

You've told your query what you want, and how many per page you want, but nowhere have you specified which page you want it to grab, so it will always grab page 1.

http://codex.wordpress.org/Class_Reference/WP_Query#Pagination_Parameters

Do this using the paged query var.

Also consider using the pre_get_posts filter instead of creating a whole new query, so that you can avoid the whole WP_Query and pagination mess completely.

2

The function get_event_list()

Should looks like this …

Whenever you use a custom query you must include paged variable in the query for pagination to work.

Pagination Note: Use get_query_var('page'); if you want your query to work in a Page template that you've set as your static front page. The query variable 'page' also holds the pagenumber for a single paginated Post or Page that includes the <!--nextpage--> Quicktag in the post content.

Important link: WP_Query:

function get_event_list( $latest = true, $order = 'ASC' ) {
    echo '<ul class="event-items">';

    global $paged; // paged variable
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; // set paged
    $yesterday = time() - 24*60*60;
    $compare = $latest ? '>' : '<';

    $args = array(
        'post_type' => 'wr_event',
        'posts_per_page' => 'posts_per_page' => is_archive() ? 16 : -1,
        'meta_key' => 'event_date',
        'orderby' => 'meta_value_num',
        'order' => $order,
        'meta_value' => $yesterday,
        'meta_compare' => $compare,
        'paged' => $paged           // add paged variable in argument array
    );

    $loop = new WP_Query( $args );
    while ( $loop->have_posts() ) : $loop->the_post();
        get_template_part( 'inc/event', 'item' );
    endwhile;
    wp_reset_postdata();
    echo '</ul>';
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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