Support

Account

Home Forums ACF PRO Best way to set this particular query up?

Unread

Best way to set this particular query up?

  • I’m using WooCommerce as an event ticketing system for a local choral group. They have anywhere from 6-8 performances every season.

    I’ve added ACF fields to the product admin page, including performance date.

    However, I’m using Bootstrap tabs to display the full season on the homepage (each performance is a separate “product”). By using the strtotime function, I’m able to at least add the active class to whatever event is > $today. So far so good.

    BUT, when we’re at the start of a season, *all* of the performances will be > $today, and will therefore *all* get the “active” status. I need *only* the next upcoming performance to get the “active” status.

    Now let’s add a bit more complication to this query: There is usually a period of 2-3 months between the end of a season and the start of a new season. During this period, I still want to display the completed season of performances, but with only the most recently-occuring performance tab having the active state applied.

    Let’s add a bit more complication:

    At the moment I’ve defined my query as pulling from a specific product category – in this case it’s a category called “2017-2018 Season” – sorted on the event date meta field. This isn’t practical down the line when we have performances to display from whatever the current season is though.

    Initially, I thought I should create a custom post type called “Seasons”, and for each Season, just have start and end date selectors, then have a post_object field in the product that allows you to choose whichever season the performance goes to. But I couldn’t figure out how to run my product query based on the post_object start & end times and THEN by the event date meta field.

    Here’s my inadequate query so far:

    <div class="nav flex-column nav-pills" id="v-pills-tab" role="tablist" aria-orientation="vertical">
       <?php $args_left = array(
          'post_type' => 'product',
          'posts_per_page' => -1,
          'product_cat' => '2017-2018-season',
          'meta_key' => 'date',
          'orderby' => 'meta_value_num',
          'order' => 'ASC'
       );
       $loop_left = new WP_Query( $args_left );
    
       $counter = 0;
    
       while ( $loop_left->have_posts() ) : $loop_left->the_post(); global $product;
    
          $today = date('Ymd');
          $date = get_field('date');
          $date = str_replace('/', '-', $date);
          $date_timestamp = strtotime($date);
          $series = get_field('concert_series');
       ?>
    
       <a class="nav-link <?php if ($date_timestamp > strtotime($today)) { ?>active<?php } else { ?><?php } ?>" id="v-pills-<?php echo $counter; ?>-tab" data-toggle="pill" href="#v-pills-<?php echo $counter; ?>" role="tab" aria-controls="v-pills-<?php echo $counter; ?>" aria-selected="<?php if ($date_timestamp < strtotime($today)) { ?>true<?php } else { ?>false<?php } ?>">
          <h3><?php echo date('M', $date_timestamp); ?>. <span><?php echo date('d', $date_timestamp); ?>, <?php echo date('Y', $date_timestamp); ?></span></h3>
          <p><?php echo $series; ?></p>
       </a>
    
       <?php $counter++;
    
       endwhile; ?>
    
    </div>
Viewing 1 post (of 1 total)

The topic ‘Best way to set this particular query up?’ is closed to new replies.