Last active
November 30, 2021 10:49
-
-
Save calvez/683d19646fdcc574d23af6ecdd29d226 to your computer and use it in GitHub Desktop.
WP_Query on diff post_types with custom conditions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Event layout | |
*/ | |
get_header(); | |
$description = get_the_archive_description(); | |
?> | |
<header class="page-header alignwide"> | |
<?php | |
require get_stylesheet_directory() . '/template-parts/header/event-header.php'; | |
?> | |
</header><!-- .page-header --> | |
<div class="event-archives mx-auto"> | |
<div class="entries m-2"> | |
<?php | |
$currentDate = date('Y-m-t'); | |
$args = array( | |
'post_type' => 'event', | |
'post_status' => 'publish', | |
'order' => 'DESC', | |
'posts_per_page' => 10, | |
'meta_query' => array( | |
array( | |
'key' => '_event_start_date', | |
'value' => $currentDate, | |
'compare' => '>=' | |
) | |
) | |
); | |
$loop = new WP_Query($args); ?> | |
<?php if ($loop->have_posts()) : ?> | |
<?php | |
while ($loop->have_posts()) : $loop->the_post(); ?> | |
<div class="event-entry pt-10"> | |
<?php get_template_part('inc/archive/event-archive', 'card'); ?> | |
</div> | |
<?php endwhile; ?> | |
<?php else : ?> | |
<h2 class="p-4 text-center"><?php echo __('No upcoming events', 'accilium_theme'); ?></h2> | |
<?php endif; ?> | |
</div> | |
<!-- archive list ends here --> | |
<?php | |
$events_q = new WP_Query( | |
array( | |
'post_type' => 'event', | |
'fields' => 'ids', | |
'nopaging' => true, | |
'order' => 'DESC', | |
'post_status' => 'publish', | |
'meta_query' => array( | |
array( | |
'key' => '_event_start_date', | |
'value' => $currentDate, | |
'compare' => '<', | |
'type' => 'DATE' | |
) | |
) | |
) | |
); | |
// var_dump($events_q); | |
$posts_q = new WP_Query( | |
array( | |
'post_type' => 'post', | |
'fields' => 'ids', | |
'nopaging' => true, | |
'order' => 'DESC', | |
'post_type' => 'post', | |
'cat' => array(89, 36), | |
'order' => 'DESC' | |
) | |
); | |
// Sum up all IDs | |
$event_items = $events_q->get_posts(); | |
$post_items = $posts_q->get_posts(); | |
$ids = array_merge($event_items, $post_items); | |
$wp_query = new WP_Query( | |
array( | |
'post_type' => array('event', 'post'), | |
'post__in' => $ids, | |
'order' => 'DESC', | |
'posts_per_page' => 12 | |
) | |
); | |
?> | |
<h2 class="p-4 text-center"><?php echo __('PAST EVENTS', 'accilium_theme'); ?></h2> | |
<?php | |
// The Loop | |
if ($wp_query->have_posts()) : ?> | |
<div class="slick-wrapper relative"> | |
<div id="posts-slider" class="slick-slider"> | |
<?php while ($wp_query->have_posts()) : ?> | |
<?php $wp_query->the_post(); ?> | |
<article> | |
<div class="slide-item"> | |
<a href="<?php the_permalink(); ?>"> | |
<figure class="slide-img"> | |
<?php | |
if (has_post_thumbnail($post->ID)) { | |
echo the_post_thumbnail('slider-thumb'); | |
} else { | |
echo '<img src="' . get_stylesheet_directory_uri() . '/assets/img/acc_280-200.png"/>'; | |
} | |
?> | |
</figure> | |
<?php | |
$event_date = get_post_meta(get_the_ID(), '_event_start_date', true); | |
// Check if the event_date field has a value. | |
if (!empty($event_date)) : ?> | |
<p class="slide-date"><?php echo date_i18n(get_option('date_format'), strtotime($event_date)); ?></p> | |
<?php else : ?> | |
<p class="slide-date"><?php echo get_the_date(); ?></p> | |
<?php endif; ?> | |
<p class="slide-title"><?php the_title(); ?></p> | |
</a> | |
</div> | |
</article> | |
<?php endwhile; ?> | |
</div> | |
<div class="slick-slider-dots"></div> | |
</div> | |
<?php wp_reset_postdata(); ?> | |
<?php endif; ?> | |
</div> | |
<?php get_footer(); ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment