WordPress.org

Ready to get started?Download WordPress

Forums

Posts 2 Posts
wp_reset_postdata() doesn't work (3 posts)

  1. Marco Panichi
    Member
    Posted 1 year ago #

    I've used the second-last example showed here:
    https://github.com/scribu/wp-posts-to-posts/wiki/each_connected

    The only difference is that I've put the the_title output after echoing the connected posts. So my code is:

    <?php
    $my_query = new WP_Query( array(
    	'post_type' => 'movie'
    ) );
    
    p2p_type( 'movies_to_actors' )->each_connected( $my_query, array(), 'actors' );
    
    p2p_type( 'movies_to_locations' )->each_connected( $my_query, array(), 'locations' );
    
    while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
    
    	<?php
    	// Display connected actors
    	echo '<p>Actors:</p>';
    
    	foreach ( $post->actors as $post ) : setup_postdata( $post );
    		the_title();
    
    		...
    	endforeach;
    
    	wp_reset_postdata();
    
    	// Display connected locations
    	echo '<p>Filming locations:</p>';
    
    	foreach ( $post->locations as $post ) : setup_postdata( $post );
    		the_title();
    
    		...
    	endforeach;
    
    	wp_reset_postdata();
    	?>
    
    	<?php the_title(); ?>
    
    <?php endwhile; ?>

    The problem is that all the posts after the first one show me the title of the first post:

    --------------------------------------------------
    Actors: actors of post title n.1
    Locations: locations of post title n.1
    TITLE n.1
    --------------------------------------------------
    Actors: actors of post title n.2
    Locations: locations of post title n.2
    TITLE n.1 <<<--- WRONG!!!
    --------------------------------------------------

    http://wordpress.org/extend/plugins/posts-to-posts/

  2. ancawonka
    Member
    Posted 1 year ago #

    Wow, that's an interesting problem. At first glance it seems like it should work, though it's kind of hard to understand what's going on.

    What have you tried to do to debug it?

    Try doing a var_dump($post) in addition to the_title() after the second wp_reset_postdata(). Is it going back to the original post int the loop?

    What happens if there are three or four titles? Does it go back to the previous one, or to the first one?

  3. mfrerebeau
    Member
    Posted 1 year ago #

    J'ai le même souci sur l'interface d'admin (édition d'un CPT...)
    La solution trouvée a été d'extraire les données de ma query sans passer par les variables globale (sans utiliser the_post ou setup_postdata) :

    <select name="mf_toom_series_link" id="mf_toom_series_link">
            <option value="" ><?php _e( 'Série de cet épisode', 'mf_toom_series' ) ?>  </option>
            <?php $series_list = new WP_Query( array( 'post_type' => 'toom_serie', 'post_status' => 'publish', 'posts_per_page' => -1 ) ); ?>
            <?php foreach( $series_list->posts AS $serie ) : ?>
                <option value="<?php echo $serie->ID ?>" <?php if( $mf_toom_series_link == $serie->ID ) : ?>selected="selected"<?php endif?> ><?php echo $serie->post_title ?> </option>
            <?php endforeach; ?>
        </select>

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic