Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

I'm using get_posts for each query and the I'm able to list the results:

<?php


  $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 );
?>

Unfortunately I don't seem to be able to get the results to paginate.

share|improve this question

1 Answer 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.

share|improve this answer
    
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

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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