Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a basic loop (a rough example of what I'm doing) of work, and want to show the client(s) the work was done for. Some work has clients connected and some doesn't. The work that doas have one or more client associated with it echos the clients paragraph, and echos it once for each connect client, so it is recognizing the connections. But it doesn't echo the title, and I cant't seem to figure out why.

This is taken straight from the Posts 2 Posts github: https://github.com/scribu/wp-posts-to-posts/wiki/each_connected

<?php
$my_query = new WP_Query( array(
    'post_type' => 'work'
) );

p2p_type( 'work_for_client' )->each_connected( $my_query, array(), 'clients' );


while ( $my_query->have_posts() ) : $my_query->the_post(); ?>

    <?php the_title(); ?>

    <?php
    // Display connected clients


    foreach ( $post->clients as $post ) : setup_postdata( $post );
        echo '<p>clients:</p>';
        echo the_title();

    endforeach;

    wp_reset_postdata();

    ?>
share|improve this question
    
What does $post->client look like? Unless it is an array of WordPress post objects (so that each $post is a post object) I doubt the_title is going to work. Looks like you are probably clobbering $post a couple of different times as well. –  s_ha_dum Nov 19 '12 at 4:50
    
$post->client doesn't work because the post type is clients, but I tried it anyway. I took this straight from the Posts 2 Posts git page: github.com/scribu/wp-posts-to-posts/wiki/each_connected –  Tiffany Israel Nov 19 '12 at 5:05

1 Answer 1

Where is your code? In a function? Somewhere in a template?

setup_postdata does not replace the global $post with the $post object passed in. It does, however, modify some other globals.

So if you're trying to use the various the_* template tags, you need to explicitly replace the global $post object on which they depend.

In a function, for example...

<?php
function wpse73103_loop_example()
{
    global $post; // this is important!

    $posts = new WP_Query(/* your args here */);

    // because $post is global above, this will
    // overwrite the global object.
    foreach($posts as $post)
    {
        setup_postdata($post);
        // do stuff
    }

    wp_reset_postdata(); // back to normal
}

Anyway, the moral of the story is that you should try putting global $post; before any of the code you posted (which is very much correct).

share|improve this answer
    
This is in a template and isn't a function. It's using the posts 2 posts plugin, so that extra query-ing is important. But I tried adding global $post; to the top and that didn't work. However I am able to get wp_alchemy meta but not a basic thing like the_title so I think your right, I just gotta figure out how to apply it to this specific code. –  Tiffany Israel Nov 19 '12 at 5:23
    
If all else fails you can use something like get_post_field‌​. You can also try doing $GLOBALS['post'] = $post; inside your foreach loop. –  chrisguitarguy Nov 19 '12 at 5:27

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.