Multiple loops on category.php WP_Query or pre_get_posts?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


Multiple loops on category.php WP_Query or pre_get_posts?



Bit of background i have an normal events category, specifically for that category there is a date meta box for the date of the event.



In the list of posts on that page category-events.php i need the upcoming posts to appear first in the list and then the events that are in the past to appear after.



So how can i combine two loops to spit out the list of posts that are upcoming first and events in the past after those.



Can you combine a pre_get_posts to do this, or just use two WP_Query on the category-events.php file?


pre_get_posts


WP_Query



pre_get_posts Query for upcoming events...


pre_get_posts


function events_query_order($query){
if( ! is_admin() && $query->is_main_query() ):
$query->set( 'cat', '3' );//Specific Event Category
$query->set( 'meta_key', 'event_date_picker' );
$query->set( 'posts_per_page', -1 );
$query->set( 'orderby', 'meta_value_num' );
$query->set( 'order', 'ASC' );
$meta_query = array(
array(
'key' => 'event_date_picker',
'compare' => '>',
'value' => date("Y-m-d"),
'type' => 'DATE'
)
);

$query->set( 'meta_query', $meta_query );


endif;
};



WP_Query query for posts in the past


WP_Query


<?php

$args = array(
'category_name' => 'events',
'meta_key' => 'event_date_picker',
'posts_per_page' => -1,
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'event_date_picker',
'compare' => '<',
'value' => date("Y-m-d"),
'type' => 'DATE'
)
)

);


$past_events_content = new WP_Query( $args );

if ( $past_events_content->have_posts() ) :


?>



Is this even achivable whilst respecting normal pagination as well?









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Arduino Mega cannot recieve any sketches, stk500_recv() programmer is not responding

Visual Studio Code: How to configure includePath for better IntelliSense results

C++ virtual function: Base class function is called instead of derived