0

I would like to sort the output of my WP Query. I want the events that are coming up soon to be at the beginning. The events that have already taken place should be at the end. So the first array in my meta_query should be output first, then the second array.

    $the_query = new WP_Query( 
        array( 
            'post_type'         => 'page',
            'post_status'       => 'publish',
            'posts_per_page'    => $posts,
            'meta_key'          => 'datum-von',
            'orderby'           => 'meta_value upcoming_events',
            'order'             => 'ASC',
            'post__in'          => array(22888,23062,23065,23348),
            'meta_query'        => array(
                'relation' => 'OR',
                'upcoming_events' => array(
                    'key' => 'datum-von',
                    'value' => date('Y-m-d'),
                    'compare' => '>=',
                    'type' => 'DATE'
                ),
                'past_events' => array(
                    'key' => 'datum-von',
                    'value' => date('Y-m-d'),
                    'compare' => '<=',
                    'type' => 'DATE'
                )
            ),
        )
    );

Does anyone have an idea how I can control the sorting? Thanks for reading!

2
  • Is there any reason you shouldn't go with 2 separate queries? Like, one for upcoming events and one for past events?
    – Abhik
    May 18 at 8:50
  • I display the data in a three-col post grid. In the loop I set the HTML tags every 3 posts so that the next row can start. This would not work with two separate queries. But if I change the layout, it would be somehow possible of course.
    – lcsqntl
    May 19 at 8:01

2 Answers 2

0

You can use get_posts() for both event types and merge them.

$merged_events = array();

$upcoming = get_posts(array(
    /* Your upcoming events args here. */
));

$past = get_posts(array(
    /* Your past events args here. */
));

$merged_events = array_merge( $upcoming, $past);

foreach( $merged_events as $merged_event ) {
    //Your loop
}
1
  • Thank you very much. I am already further along in my solution. Now I just try to use the posts_per_page after the merge.
    – lcsqntl
    May 23 at 8:30
0

Thanks for the input! That's how I built my code now. But the "posts_per_page" can't be controlled by the arrays now. It will always output all posts.

add_shortcode( 'display_events', 'display_events' );
function display_events( $atts ) {
    
    extract( shortcode_atts( array (
        'posts' => -1,
    ), $atts ) );
    
    $args1 = array(
            'post_type'         => 'page',
            'post_status'       => 'publish',
            'posts_per_page'    => $posts,
            'meta_key'          => 'datum-von',
            'orderby'           => 'meta_value',
            'order'             => 'ASC',
            'post__in'          => array(22888,23062,23065,23348),
            'meta_query'        => array(
                array(
                    'key' => 'datum-von',
                    'value' => date('Y-m-d'),
                    'compare' => '>=',
                    'type' => 'DATE'
                )
            )
    );
 
    $loop1 = new WP_Query($args1);
 
    $args2 = array(
            'post_type'         => 'page',
            'post_status'       => 'publish',
            'posts_per_page'    => $posts,
            'meta_key'          => 'datum-von',
            'orderby'           => 'meta_value',
            'order'             => 'ASC',
            'post__in'          => array(22888,23062,23065,23348),
            'meta_query'        => array(
                array(
                    'key' => 'datum-von',
                    'value' => date('Y-m-d'),
                    'compare' => '<=',
                    'type' => 'DATE'
                )
            )
    );
 
    $loop2 = new WP_Query($args2);

    $the_query = new WP_Query();
    $the_query->posts = array_merge($loop1->posts, $loop2->posts);
    $the_query->post_count = $loop1->post_count + $loop2->post_count;

        while ($the_query->have_posts()) {
            $the_query->the_post();   
            
            // display the data
            the_title();
        } else {
        // no posts found
        }
    wp_reset_postdata();
}

Any idea how to limit the posts_per_page in this situation?

3
  • You can not control pagination when you merge two separate queries. Being said that, it's possible, but will put a lot overhead. You can store all the posts in a session array for a user and create custom pagination. It will work for small amount of posts. The ideal solution would be using array in orderby parameter.
    – Abhik
    May 23 at 12:32
  • Why don't you just get the posts and compare the meta value with today's date within the loop?
    – Abhik
    May 23 at 12:35
  • Hi @lcsqntl - please add supplementary information into your question or ask a new question instead of posting this as an answer.
    – Bysander
    May 23 at 16:45

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.