0

I have an events site where I want to display upcoming events that can be booked in ascending date order, and events that are closed for registration in descending order.

I'm using get_posts for each query and while I'm able to list the results, I don't seem to be able to get the results to paginate:

$posts = get_posts( 
    array(
        'post_type' => 'espresso_event',
        'meta_key'  => 'event_start_date',
        'orderby' => 'meta_value',
        'order' => 'ASC',
        'posts_per_page' => '10',
        'paged' => $paged,
        'meta_query' => array(
            array(
                'key' => 'event_registration_end',
                'value' => date('Y-m-d'),
                'compare' => '>='
            )
        ) 
    ) 
);
$moreposts = get_posts( 
    array(
        'post_type' => 'espresso_event',
        'meta_key'  => 'event_start_date',
        'orderby' => 'meta_value',
        'order' => 'DESC',
        'posts_per_page' => '10',
        'paged' => $paged,
        'meta_query' => array(
            array(
                'key' => 'event_registration_end',
                'value' => date('Y-m-d'),
                'compare' => '<='
            )
        )
    )
);
$posts = array_merge( $posts, $moreposts );
foreach( $posts as $post ) : setup_postdata( $post );
1

Firstly you need to merge your two query into one,
and need to use WP_Query instead of get_posts:

$paged = 1 ;
$posts_per_page = 10 ;

$args = array(
    'post_type' => 'espresso_event',
    'meta_key'  => 'event_start_date',
    'orderby' => 'meta_value',
    'order' => 'ASC',
    'posts_per_page' => $posts_per_page,
    'paged' => $paged,
    'meta_query' => array(
        array(
            'key' => 'event_registration_end',
            'value' => date('Y-m-d'),
            'compare' => '>='
        ),
        array(
            'key' => 'event_registration_end',
            'value' => date('Y-m-d'),
            'compare' => '<='
        )
    )
) ;

$query = new WP_Query($args) ;
if ($query->have_posts()) {
    // Here our parameter:
    $total = $query->found_posts ;
    while ($query->have_posts()) {
        $query->the_post() ;
        // Do something like
        ?><h3><?php the_title() ; ?></h3><?php
    }
    echo '<div id="pagination">' ;
    $next_link = '' ;
    $prev_link = '' ;
    if ($paged > 1) {
        if ($paged-2) {
            $prev_link = add_query_arg(array(
                'paged' => $paged-1,
            ),get_permalink($post->id)) ;
        } else {
            $prev_link = get_permalink($post->id) ;
        }
    }
    if (($total/$posts_per_page) > $paged) {
        $next_link = add_query_arg(array(
            'paged' => $paged+1,
        ),get_permalink($post->id)) ;
    }
    if ($next_link) {
        echo '<a href="'.$next_link.'" class="next">Next Page</a>' ;
    }
    if ($prev_link) {
        echo '<a href="'.$prev_link.'" class="prev">Previous Page</a>' ;
    }
    echo '</div>' ;
} else {
     echo 'Sorry, there is no post etc' ;
}

Not tested, but your need must be something like this.

  • It doesn't display any of the posts. The reason I didn't go with WP_Query is that I want to display all the bookable events first in ASC and then the non bookable events in DESC. The best way to do this seems to be to merge the queries. Which I can't seem to do as a WP_Query. – Josh Feb 6 '13 at 22:44
  • @Josh As I know get_posts not support to return total post number, you need to use WP_Query still, than merge two total numbers – Digerkam Feb 7 '13 at 10:57
  • @Josh Or "query_posts": stackoverflow.com/questions/11086315/… – Digerkam Feb 7 '13 at 11:08
  • I have decided to use jQuery to paginate the posts. There are not more than 40 so it seems like the best approach. Thanks for your help. – Josh Feb 14 '13 at 13:48
  • Well, I think you dont need jQuery for that. get_posts never give total post number, so then you couldnt build a pagination properly. I have looked it several times, but no proper solution for get_posts. Please share your jQuery solution when you did. – Digerkam Feb 14 '13 at 14:43

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.