$wp_query
var_dump($wp_query) – wyświetla wszystko
$wp_query->post_count – liczba postów
pobranie custom post event w kolejności daty rozpoczęcia z wykluczeniem wydarzeń których data końcowa już minęła w function.php:
function get_events_posts() { $args = array( 'post_type' => 'event', 'meta_key' => 'start', 'orderby' => 'meta_value', 'order' => 'ASC', 'posts_per_page' => 4, 'meta_query' => array( array( 'key' => 'end', 'value' => date('Y-m-d'), 'compare' => '>=', 'type' => 'DATE' ) ), ); // The Query $queryEvents = new WP_Query($args); return $queryEvents; }
w kodzie strony zamieszczamy:
<?php $query = get_events_posts(); ?> <?php if ( $query->have_posts() ) : ?> <?php while ( $query->have_posts() ) : $query->the_post(); ?> <div class="home-box-events-pic-output"> <?php if ( has_post_thumbnail() ) : the_post_thumbnail('event-image'); ?> <?php endif; ?> </div> <?php endwhile; ?> <?php endif; wp_reset_postdata(); ?>
—
lub kiedy chcielibyśmy zawęzić obszar wyświetlanych eventów do konkretnego zakresu:
$start = '2011-11-31'; $end = '2011-10-01'; $args = array( 'post_type' => 'events', 'posts_per_page' => -1, 'orderby' => 'meta_value', 'order' => 'ASC', 'meta_key' => '_my-datetime-from', 'meta_query' => array( array( 'key' => '_my-datetime-from', 'value' => array($start, $end), 'compare' => 'BETWEEN', 'type' => 'DATE' ) ) ); // Make the query $events_query = new WP_query(); $events_query->query($args);
funkcja do zawężania $query
function if_adjust_queries($query) { if ( !is_admin() && is_post_type_archive('event') && $query->is_main_query() ) { $query->set('posts_per_page', '1'); } } add_action('pre_get_posts', 'if_adjust_queries');
Wykaz z postami powiązanymi (related post ACF) z uwzględnieniem daty eventów:
$today = date('Ymd'); $relatedEvents = new WP_Query(array( 'posts_per_page' => -1, 'post_type' => 'event', 'meta_key' => 'event_date', 'orderby' => 'meta_value', 'order' => 'ASC', 'meta_query' => array( array( 'key' => 'event_date', 'compare' => '>=', 'value' => $today, 'type' => 'numeric' ), array( 'key' => 'related_programs', 'compare' => 'LIKE', 'value' => '"' . get_the_ID() . '"', ) ) )); while ( $relatedEvents->have_posts() ) { $relatedEvents->the_post(); ?> <li><h2><a href="<?php echo get_permalink(); ?>"><?php the_title(); ?></a></h2></li> <?php } wp_reset_postdata(); ?>
—
'post__not_in’ => array($donotrepeat),
Hope this helps and happy coding :)