Support

Account

Home Forums General Issues Use post object in a loop of a custom post type

Solving

Use post object in a loop of a custom post type

  • Hi!

    I created a loop for a custom post type. This custom post type has a post object field which links to another custom post type. So inside the loop of the custom post type i need to show data from the linked post. Actually after i called the post-object i cant get back to the default loop object after i reset the postdata. How can i after i show data from the linked data, again show data from the loop object?

    <?php $loop = new WP_Query($args); ?>
    <?php if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post(); ?>
    <?php
    
    // HERE STARTS THE POST OBJECT PART TO SHOW DATA FROM THE LINKED POST 
    $post_object = get_field('kurs_zu_diesem_seminar');
                                    
    if( $post_object ): 
    
    // override $post
    $post = $post_object;
    setup_postdata( $post ); 
    ?>
    
    <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
    
    <?php wp_reset_postdata(); ?>
    <?php endif; ?>
    // END POST OBJECT PART
    
    // START SHOW DATA FROM DEFAULT OBJECT
    <span class="title"><?php the_title(); ?> am <?php the_field('start_datum'); ?></span>
    <?php the_field('beschrieb'); ?>
    
    <?php endwhile; ?>
    <?php else: ?>
    <p>Aktuell sind keine Seminare geplant.</p>
    <?php endif; ?>
                                    
    <?php wp_reset_postdata(); ?>   
  • Hi @leuchterits

    I tested your code on my end and it works great. Could you please share the JSON or XML export of your field group so I can check your setup?

    Also, you can always use the returned post as a post object like this:

    <?php $post_object = get_field('kurs_zu_diesem_seminar'); ?>
    
    <h3><a href="<?php echo get_permalink($post_object->ID); ?>"><?php echo $post_object->post_title; ?></a></h3>

    I hope this helps. Thanks 🙂

  • Hi,
    I had the same problem.
    I made a loop on CPT and in each one I need a post-objet.

    Here is what i made

    <table>
    <tr>
    <td>Nom/Prénom</td><td>Numéro de téléphone</td><td >email</td><td>Commission</td>
    </tr>    
            
         
    <?php	while($author_posts_contacts->have_posts()) : $author_posts_contacts->the_post();?>
        <tr>
        	<td> <?php $id_post_origine=get_the_ID();?>//Register the "parent" post id
            <strong><?php the_field('titre_du_post'); ?> <?php the_field('nom_correspondant_festo'); ?></a></strong></br>
    		<?php $post_object = get_field('structure'); if( $post_object ): 	$post = $post_object; setup_postdata( $post );	?>//Call the post object Inside and work with
    (<?php the_title(); ?>)
    <?php $post = $id_post_origine; setup_postdata( $post ); ////Call back the "parent" post datas
    
    endif; ?>
            </td>
            <td><?php the_field('numero_de_telephone_correspondant_festo'); ?></td>
            <td><?php the_field('email_correspondant_festo'); ?></td>
            <td><?php $mots_cles = get_field('commission_correspondant_festo');
    		foreach ($mots_cles as $mot_cle):
    		$id_du_mot_cle = get_term( $mot_cle);
    		echo $id_du_mot_cle->name.'</br>';
    		endforeach;?>
            
    		
    </td></tr>
               
        <?php           
        endwhile;?></table>

    I hope this helps.

  • <?php wp_reset_postdata(); ?>
    ↓
    <?php $loop->reset_postdata(); ?>

    <?php $loop = new WP_Query($args); ?>
    <?php if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post(); ?>
    <?php
    
    // HERE STARTS THE POST OBJECT PART TO SHOW DATA FROM THE LINKED POST 
    $post_object = get_field('kurs_zu_diesem_seminar');
                                    
    if( $post_object ): 
    
    // override $post
    $post = $post_object;
    setup_postdata( $post ); 
    ?>
    
    <h3><a>"><?php the_title(); ?></a></h3>
    
    <?php $loop->reset_postdata(); ?>
    <?php endif; ?>
    // END POST OBJECT PART
    
    // START SHOW DATA FROM DEFAULT OBJECT
    <span class="title"><?php the_title(); ?> am <?php the_field('start_datum'); ?></span>
    <?php the_field('beschrieb'); ?>
    
    <?php endwhile; ?>
    <?php else: ?>
    <p>Aktuell sind keine Seminare geplant.</p>
    <?php endif; ?>
                                    
    <?php wp_reset_postdata(); ?>
Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Use post object in a loop of a custom post type’ is closed to new replies.