Home › Forums › General Issues › 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:
$post
is being populated as intended?$post
is “ID” and not “id” or “post_id”?get_the_ID()
you are asking for the ID of the current main loop item, for which you have called the template?You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
ACF wouldn’t be so widely used in WordPress if it didn’t have some pretty amazing capabilities. In this article, we look at a few of the features we’ll discuss during “7 things you didn’t know you could do with ACF” at #WPEDecode later this month. https://t.co/5lnsTxp81j pic.twitter.com/Yf0ThPG1QG
— Advanced Custom Fields (@wp_acf) March 16, 2023
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.