Home Forums ACF PRO Get Next Post Reply To: Get Next Post

  • I’ve looked at your code a couple of times and to be honest, I can’t really figure out what you’re trying to do. This is your code with the white space cleaned up so I could see what’s going on a little better and some comments.

      if (have_rows('tradeshows', 40)):
        // this field is getting nothing
        // because there is no while(have_rows()) loop or call to the_row()
        // since $current_event_date has a value of NULL the query will not return anything
        // or maybe since the value is NULL it returns all posts
        // not really sure here
        $current_event_date = get_sub_field('event_date');
        $next_event_query = new WP_Query(array(
          'post_type'      => 'event',
          'post_status'    => 'publish',
          'posts_per_page' => -1,
          'meta_query'     => array(
            'key'        => 'event_date',
            'compare'    => '>',
            'value'      => $current_event_date,
          'meta_key'     => 'event_date',
          'orderby'        => 'meta_value',
          'order'          => 'ASC'
        // not sure why we're getting this field here
        // this variable is never used
        $rows = get_field('tradeshows', 40);
          <div class="trades">
              if ($next_event_query->have_posts()) : 
                // this is looping through the posts of the nested query
                // show values associated with those posts
                // if the query above returns nothing then this will never run
                while ($next_event_query->have_posts()): 
                    <div class="col-sm-3">
                      <h3>NEXT TRADESHOW</h3>
                      <p><?php echo $current_event_date; ?></p>
            <div class="col-sm-6 col-sm-offset-3">        
              <h3>UPCOMING TRADESHOWS</h3>
                while (have_rows('tradeshows', 40)): 
                  // this is looping through the original repeater
                  // to show subfield of this field for post ID = 40
                    <div class="col-sm-4">                       
                        <p class="eventdate"><?php the_sub_field('event_date'); ?><br />
                        <span class="locplace"><?php the_sub_field('event_location'); ?></span></p>

    Looking back over everything, I honestly can’t tell how the event date in post 40 is related to the “event_date” in your query where you’re trying to get the posts, or what type of field that is or how it should be queried. Is it another sub field of a repeater in those other posts? Is it a top level field? Why is <?php if( have_rows('tradeshows', 40) ): ?> looking at post ID 40 in the first place?

    The first thing you need to do is to go back to a point where the loop for the repeater is working, showing the current values of that repeater and then explain what it is you’re trying to query for in the other posts. Like I said, is it a top level field? is it a sub field of a repeater?