Home › Forums › Front-end Issues › Displaying Post Object Title in Query
Hi there
I am trying to display the title of of post_object field inside an custom query loop.
Loop works as expected (showing 3 different posts) as long as I am not trying to show the post object field. Then, loop shows 3 identicals posts of itself…
see http://delphire.myhostpoint.ch/reiseangebote/ (at the bottom “PAUSCHALANGEBOTE”)
<section id="pauschalangebote">
<h2>Pauschalangebote</h2>
<div class="row">
<?php $custom_query01 = new WP_Query('post_type=reisen&posts_per_page=3&category_name=pauschalangebote'); ?>
<?php while($custom_query01->have_posts()) : $custom_query01->the_post(); ?>
<div class="col-md-4">
<!-- ==== this is where title of post object should appear === -->
<?php $post_object01 = get_field('reisen_reederei');
if( $post_object01 ):
// override $post
$post = $post_object01;
setup_postdata( $post ); ?>
<?php the_title(); ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
<!-- ========= this is the end of it ======== -->
<p><?php the_post_thumbnail('delphi_4-3', array('class' => 'darken img-fluid')); ?></p>
<small><?php the_field('reisen_datum_start'); ?> – <?php the_field('reisen_ende'); ?></small>
<h4><a class="black" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
<?php the_excerpt(); ?>
<a href="<?php the_permalink(); ?>" class="btn btn-warning">Daten & Preise</a>
</div><!-- /.col-md-4 -->
<?php endwhile; ?>
<?php wp_reset_postdata(); // reset the query ?>
</div><!-- /.row -->
</section>
any ideas? Thanks in advance for your help!
Hi,
Not sure about what’s wrong but I already have this issue ( When get post object inside a post query and display data). I solve it by register each post ID and get data with the ID ( I think WP is a bit confused =D) …
Try to save the current query post ID :
...
<?php while($custom_query01->have_posts()) : $custom_query01->the_post(); ?>
<?php $query_id = get_the_ID(); // THE CURRENT POST OF QUERY ID ?>
...
Do the same for the object you get :
...
if( $post_object01 ):
$object_id = $post_object01->ID; // THE OBJECT ID
...
And display all your data with echo and “get” like that :
...
<?php echo get_the_title($object_id); // USE THE ID ?>
...
<a href="<?php echo get_the_permalink($query_id); ?>" class="btn btn-warning">Daten & Preise</a>
...
…almost there!
i managed to get most of the output running with echo end get.
except:
<p><?php echo get_the_excerpt($query_id); ?></p>
(get_the_excerpt doesn’t seem to work with $query_id as long as it is not a dedicated excerpt)
the “echo and get” solution is normally a “not in loop” solution. Unfortunatelly, outside a loop (so, in our case), if your not having a “created user excerpt” get_the_excerpt is empty. If you just want a “cut” content to make an excerpt you can use this :
<?php
$content_post = get_post($query_id);
$content = $content_post->post_content;
$excerpt = wp_trim_words( $content , 20, '...');
echo $excerpt;
?>
Where “20” is the char’ length of the excerpt you want, and “…” the text after the excerpt if the content was cut.
The topic ‘Displaying Post Object Title in Query’ is closed to new replies.
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!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.