简体   繁体   中英

ACF Relationship Field: parent conditional based on nested conditional

I have a CPT called Classes. Via an ACF Relationship field I'm allowing my client to hand pick classes to show on the front end. Each class has an expiry date.

Within a foreach statement I set up a conditional that compares the current date with the expiry date and only shows upcoming classes. What I need is to show a single note saying "there are no upcoming classes" once all the selected classes have gone past their expiry date.

ACF support suggested adding an incremental operator within the foreach loop and then checking if that value is empty. They modified my code as follows but it doesn't do the job. Additional help from ACF support falls outside the scope of what they offer, so I'm posting here for guidance. Thanks!

<?php 
    $all_classes = get_sub_field('class');
    if( $all_classes ):
?>

    <?php
        $i = 0;
        foreach($all_classes as $post):
        setup_postdata($post);
    ?>

        <?php
            $now = time(); // get today's date
            $expiry_date = strtotime(get_field('class-expiry-date')); // get the expiration date
            if ($now < $expiry_date): // compare the dates and show upcoming classes only
            $i++;
        ?>

            class details

        <?php endif; ?>

    <?php
        endforeach;
        wp_reset_postdata();
    ?>

<?php else: ?>

    <?php
        //check if $i is empty
        if(empty($i)):
    ?>

        There are no upcoming classes.

    <?php endif; ?>

<?php endif; ?>

Iv'e ran into this many times. My approach is a little different. Is use a normal WP_Query() and make sure the ACF relationship postobject field not saving the handpicked items as post object but as an post ID . (this is just an option to select on the custom field page). In this way I can make use of the native Wordpress meta queries

My args array is as follows allowing the query to sort on an specific date field that is equal or newer than today.

 $args = array(
    'post_type'       => 'class', /* (your CPT slug: class or classes) */
    'posts_per_page'  => -1,
    'meta_key'        => 'class-expiry-date',
    'orderby'         => 'meta_value',
    'order'           => 'ASC',
    'post__in'        => array( get_sub_field( 'class' ) ),     
    'meta_query'      => array(
      array(
        'key'     => 'class-expiry-date',
        'value'   => date('Ymd', strtotime('now')),
        'type'    => 'date',
        'compare' => '>=',
      )
    )
  );

  $wp_query = new WP_Query( $args );

  if( $wp_query->have_posts() ) {
    while( $wp_query->have_posts() ) {
      $wp_query->the_post();
      // Upcoming classes !!
      // echo $post->post_title (example)
    }
  } else {
    // There are no upcoming classes
  }

  wp_reset_query();

Make sure you output the custom expiry date field in the following format Ymd (also an option on the custom fields page)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM