0

Summary: I have a page template with 3 queries.

  1. Current Exhibition
  2. Upcoming Exhibition
  3. Past Exhibition

The dates are custom fields. The problem I'm having is, how do I dynamically exclude the current post date from the upcoming exhibition query. After some digging around, I found I could use 'post__not_in'. Some examples below:

Example 1

$args  = array(
    'post_type'      => 'post',
    'posts_per_page' => 1,
    'cat'            => 14,
    'post__not_in'   => array( 71, 1 ),
    'orderby'        => 'date',
    'order'          => 'ASC',
    'post_status'    => 'publish',
);
$the_query = new WP_Query( $args )

;

Example 2

$args = array(
    'numberposts' => 5,
    'offset' => 0,
    'category' => 7,
    'post__not_in' => array( $post->ID )
);
$myposts2 = get_posts($args);

What is the proper usage for post not in to work with custom field dates? My code below -- Query Two "Upcoming Exhibitions"

<?php
/**
 * The template for displaying archive pages.
 * 
 * Template Name: Exhibition 
 *
 * Learn more: http://codex.wordpress.org/Template_Hierarchy
 *
 * @package boxpress
 */

get_header(); ?>

  <?php // require_once('template-parts/banners/banner--artist.php'); ?>

  <!-- // Query One -- pulls in current event   -->

  <?php 
   $exhibition_start_date = get_field('exhibition_start_date');
   $exhibition_end_date = get_field('exhibition_end_date');
   ?>

  <?php
  wp_reset_query();
  $exhibition_current_query = array(
    'post_type' => 'exhibition',
    'meta_key' => 'exhibition_start_date', // name of custom field
    'orderby' => 'meta_value',
    "order" => 'ASC',
    "posts_per_page" => 1,
    'meta_query' => array(
          array(
            'key' => 'exhibition_start_date',
            'value' => date('Y-m-d'),
            'compare' => '>=',
            'type' => 'DATE'
          )
        ),
    );
  $exhibition_current_loop = new WP_Query( $exhibition_current_query );
  ?>


<section class="section">
  <div class="wrap">
    <h2>Current Exhibitions</h2>
    <?php if ( $exhibition_current_loop->have_posts() ) : ?>
    <div class="l-grid l-grid--four-col">
      <?php while ( $exhibition_current_loop->have_posts() ) : $exhibition_current_loop->the_post(); ?>


       <div class="l-grid-item">
        <div style="display: flex;" class="date">
          <span class="start"><?php the_field('exhibition_start_date') ?></span> -
          <span class="end"><?php the_field('exhibition_end_date') ?></span>
        </div>
        <h4><?php the_title() ?></h4>
         <?php // get_template_part( 'template-parts/content/content-staff' ); ?>
       </div>

      <?php endwhile; ?>  
    </div>
    <?php endif; ?>
    <?php wp_reset_query(); ?>
    

    <!-- // Query Two -- pulls in upcoming event  -->

    <?php
    wp_reset_query();
    $exhibition_upcoming_query = array(
      'post_type' => 'exhibition',
      'orderby' => 'meta_value',
      'order' => 'ASC',
      'posts_per_page' => 3,
      'meta_query' => array(
            array(
              'key' => 'exhibition_start_date',
              'value' => date('Y-m-d'),
              'compare' => '>',
              'type' => 'DATE',
            )
          ),
      );
    $myposts2 = get_posts($exhibition_upcoming_query);
    $exhibition_upcoming_loop = new WP_Query( $exhibition_upcoming_query );
    ?>

    <h2>Upcoming Exhibitions</h2>

    <?php if ( $exhibition_upcoming_loop->have_posts() ) : ?>
    <div class="l-grid l-grid--four-col">
      <?php while ( $exhibition_upcoming_loop->have_posts() ) : $exhibition_upcoming_loop->the_post(); ?>


       <div class="l-grid-item">
      <div style="display: flex;" class="date">
        <span class="start"><?php the_field('exhibition_start_date') ?></span> -
        <span class="end"><?php the_field('exhibition_end_date') ?></span>
      </div>
      <h4><?php the_title() ?></h4>
         <?php // get_template_part( 'template-parts/content/content-staff' ); ?>
       </div>

      <?php endwhile; ?>  
    </div>
    <?php endif; ?>

    <!-- // Query Three -- pulls in past event  -->

    <?php
    wp_reset_query();
    $exhibition_past_query = array(
      'post_type' => 'exhibition',
      'orderby' => 'meta_value',
      "order" => 'DESC',
      "posts_per_page" => 8,
      'meta_query' => array(
            array(
              'key' => 'exhibition_start_date',
              'value' => date('Y-m-d'),
              'compare' => '<',
              'type' => 'DATE'
            )
          ),
      );
    $exhibition_past_loop = new WP_Query( $exhibition_past_query );
    ?>

    <h2>Past Exhibitions</h2>

    <?php if ( $exhibition_past_loop->have_posts() ) : ?>
    <div class="l-grid l-grid--four-col">
      <?php while ( $exhibition_past_loop->have_posts() ) : $exhibition_past_loop->the_post(); ?>


       <div class="l-grid-item">
        <div style="display: flex;" class="date">
          <span class="start"><?php the_field('exhibition_start_date') ?></span> -
          <span class="end"><?php the_field('exhibition_end_date') ?></span>
        </div>
        <h4><?php the_title() ?></h4>
         <?php // get_template_part( 'template-parts/content/content-staff' ); ?>
       </div>

      <?php endwhile; ?>  
    </div>
    <?php endif; ?>


  </div>
</section>

<?php get_footer(); ?>

1 Answer 1

0

I think you are asking how to exclude the current exhibition from your upcoming exhibitions query.

If that is the case, simply insert the ID of the current exhibition into the query using 'post__not_in':

$exhibition_upcoming_query = array(
  'post_type' => 'exhibition',
  'orderby' => 'meta_value',
  'order' => 'ASC',
  'posts_per_page' => 3,
  'post__not_in' => array( $exhibition_current_loop->posts[0]->ID ),
  'meta_query' => array(
        array(
          'key' => 'exhibition_start_date',
          'value' => date('Y-m-d'),
          'compare' => '>',
          'type' => 'DATE',
        )
      ),
  );

Hit me up if I am misunderstanding what you are after.

2
  • Yes! Sorry for the delay in response. This is exactly what I wanted. I tested today and it works. Thank you so much! Commented Feb 16, 2023 at 16:23
  • Thanks, @ChrisBrennan . Please mark the answer as correct when you get a chance.
    – kosher
    Commented Feb 17, 2023 at 18:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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