Support

Account

Home Forums General Issues Getting ACF post_object inside wp_query

Helping

Getting ACF post_object inside wp_query

  • Is it possible to use the post_object field inside custom wp_query? I created a template to list all blog posts and getting different content by getting a post_format. In one of the formats, I want to get post_object from Custom Post type that I was created. What I’m struggling with is getting ACF field values from that post, even if I pass the second parameter (ID) in the_field() it gets me always fallback, for example, for a date, it always showing me today’s date.

    Here is Query code:

     $args = array(  
              'post_type' => 'post',
              'post_status' => 'publish',
              'posts_per_page'=> 3,
              'paged' => $paged
          );
    
        $loop = new WP_Query( $args ); 
        if ( $loop->have_posts() ) :
        while ( $loop->have_posts() ) : $loop->the_post(); 
        ?>
    
             <?php get_template_part( 'template-parts/content', get_post_format() ); ?>
    
        <?php
                endwhile;
            
                wp_reset_postdata();  ?>
            <?php else : ?>
              <?php esc_html_e( 'There is no post to display' ); ?>
    
            <?php endif; ?>

    In php file of link format I’m checking if user check that he want to load post from CPT or to enter information manually. As you can see HTML and ACF fields are the same, just if user check that field, all content is loading from CPT

    <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    
          <a href="<?php echo get_permalink(); ?>">
           <h4><strong><?php the_title(); ?></strong></h4>
         </a>
        <?php 
        if ( get_field( 'dafed_promo' ) == 1 ) :
          $post_object = get_field( 'choose_dafed' );
          if ( $post_object ):
             $post = $post_object;
             setup_postdata( $post ); ?> 
    
          <?php if ( have_rows( 'event_details' ) ) : ?>
             <?php while ( have_rows( 'event_details' ) ) : the_row(); ?>
              <div class="event-info">
                 
    
                       <?php  // Load field value and convert to numeric timestamp.
                        $unixtimestampdafed = strtotime( get_sub_field( 'event_time_and_date' ) );
                     ?>
                       <div class="date">
                        <div class="month"> <?php echo date_i18n( "M", $unixtimestampdafed ); ?></div>
                        <div class="number"><?php echo date_i18n( "d", $unixtimestampdafed ); ?></div>
                      </div>
    
                
                  <strong>Vreme: </strong> <?php the_sub_field( 'event_time' ); ?><br>
                  <strong>Lokacija: </strong><?php the_sub_field( 'event_location' ); ?><br>
                  <strong>Organizator: </strong> <?php the_sub_field( 'event_organizer' ); ?>
                
    
              </div>
              <span class="path"><strong><a>
               <?php echo get_the_author(); ?></a></strong> / <?php echo get_the_date(); ?>/ 
                 <strong>
                  <?php 
                    $post_id  = get_the_ID();
                    $post_categories = get_the_category( $post_id );
                    foreach  ($post_categories as $category) {
                      echo  '<a class="category-name">' . $category->cat_name . '</a> ';
                    }
                  ?>
               </strong>
               </span>
              <?php the_excerpt(); ?>
    
            <?php endwhile; ?>
            <?php endif; ?>
          <a href="<?php echo get_permalink(); ?>" class="btn btn-border">Pročitaj više</a>
          <?php wp_reset_postdata(); ?>
          <?php endif; ?>
    
        <?php else : ?> 
    
          <?php if ( have_rows( 'event_details' ) ) : ?>
             <?php while ( have_rows( 'event_details' ) ) : the_row(); ?>
              <div class="event-info">
                 
    
                       <?php  // Load field value and convert to numeric timestamp.
                        $unixtimestamp = strtotime( get_sub_field( 'event_time_and_date' ) );
                     ?>
                       <div class="date">
                        <div class="month"> <?php echo date_i18n( "M", $unixtimestamp ); ?></div>
                        <div class="number"><?php echo date_i18n( "d", $unixtimestamp ); ?></div>
                      </div>
    
                
                  <strong>Vreme: </strong> <?php the_sub_field( 'event_time' ); ?><br>
                  <strong>Lokacija: </strong><?php the_sub_field( 'event_location' ); ?><br>
                  <strong>Organizator: </strong> <?php the_sub_field( 'event_organizer' ); ?>
                
    
              </div>
              <span class="path"><strong><a>
               <?php echo get_the_author(); ?></a></strong> / <?php echo get_the_date(); ?>/ 
                 <strong>
                  <?php 
                    $post_id  = get_the_ID();
                    $post_categories = get_the_category( $post_id );
                    foreach  ($post_categories as $category) {
                      echo  '<a class="category-name">' . $category->cat_name . '</a> ';
                    }
                  ?>
               </strong>
               </span>
              <?php the_excerpt(); ?>
    
            <?php endwhile; ?>
            <?php endif; ?>
          <a href="<?php echo get_permalink(); ?>" class="btn btn-border">Pročitaj više</a>
       <?php endif; ?>
    </article><!-- #post-## -->
  • I do not see you using the second argument anywhere. Assuming that event_details is a field of $post, you should pass the post ID at least to have_rows(). In case this is not it, we should find out where exactly the problem occurs:

    • Have you checked if $post is being populated as intended?
    • Have you checked if the ID property of $post is “ID” and not “id” or “post_id”?
    • Have you factored in that when you call get_the_ID() you are asking for the ID of the current main loop item, for which you have called the template?
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Getting ACF post_object inside wp_query’ is closed to new replies.