Home Forums Front-end Issues Displaying Post Object Title in Query


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 (at the bottom “PAUSCHALANGEBOTE”)

    		<section id="pauschalangebote">
    			<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 -->

    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>
  • ok, this sounds good. I’ll try…

  • …almost there!

    i managed to get most of the output running with echo end get.

    <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 :

    $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.

  • awesome! thank you so much!

Viewing 6 posts - 1 through 6 (of 6 total)

The topic ‘Displaying Post Object Title in Query’ is closed to new replies.