Support » Plugins and Hacks » Posts 2 Posts » [Plugin: Posts 2 Posts] Can't display connected posts, Am I doing this correct?

  • Resolved cw17s0n

    (@cw17s0n)


    In the admin side everything seems to be good.
    But I just can’t seem to get things to display.

    This is the query on the page.

    <?php
    	global $post;
            $args = array( 'numberposts' => 1, 'post_type' => 'news', 'category_name' => 'sotm', 'post_status' => 'publish' );
    	$homeSOTMArticle = get_posts( $args );
    	foreach( $homeSOTMArticle as $post ) :	setup_postdata($post); ?>
    	<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    	<p><?php the_date('F Y'); ?></p>
    	<p><?php the_excerpt(); ?></p>
    <?php endforeach; ?>
    <?php // Reset Post Data
    wp_reset_postdata(); ?>

    The name I used when I registered the connection in functions.php was ‘news_to_suppliers’, I grabbed the code from the basic usage page for displaying connected posts, dropped in ‘news_to_suppliers’. Nothing happens when I do this.

    Any insight is greatly appreciated.

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

Viewing 11 replies - 1 through 11 (of 11 total)
  • Plugin Author scribu

    (@scribu)


    I grabbed the code from the basic usage page for displaying connected posts, dropped in ‘news_to_suppliers’.

    It depends where you put it. get_queried_object_id() returns different things for different templates.

    I’m using a copy of the main template file, the only things on the page besides the query above are get_header & get_footer. I had put the code from the basic usage page after the endforeach of the query above, and before wp_reset_postdata.

    Plugin Author scribu

    (@scribu)


    The name of the template file matters. get_queried_object_id() will return a post id in single.php, a category id in category.php and so on and so forth.

    Thanks, makes sense now.

    I have a similar problem, but I can’t figure out what the solution was here. Can you guys explain what you did to fix the issue?

    It really depends of what you are doing, at which place of your theme … but you may try to replace get_queried_object_id() with $post->ID and see if that helps.

    Thanks! I ended up just going with this. I feel like there’s a better way, but I can’t use a typical query because of some of my other requirements, so this is what I’m going with, inside my foreach:

    <h4><?php $connected = p2p_type( 'clients_to_work-samples' )->get_connected( $post->ID ); while ( $connected->have_posts() ) : $connected->the_post(); the_title(); endwhile; ?></h4>

    Plugin Author scribu

    (@scribu)


    Thanks, Scribu. I had somehow missed that post in the wiki. Working as intended now!

    One question left that I’ve struggled with…and it should be easy–after you loop the loop, is there a way to still output data from the original query?

    foreach( $clients as $post ) : setup_postdata($post);
            $connected = p2p_type( 'clients_to_work-samples' )->get_connected( $post->ID );
            while ( $connected->have_posts() ) : $connected->the_post();
                  //do all $connected stuff here
           endwhile;
    
          //now do stuff with original $clients loop
          the_title();
    endforeach;

    How do you get out of the connections loop back into the original query?

    Plugin Author scribu

    (@scribu)


    Since $clients is the array you’re looping over, you can call setup_postadata( current( $clients ) ); right before //now do stuff with original $clients loop.

    Do I nee to use that in a while or foreach? Nothing changes when I use it just as you wrote it (minus the little typo)–I still get the titles from the connected elements. Code is now:

    foreach( $clients as $post ) : setup_postdata($post);
            $connected = p2p_type( 'clients_to_work-samples' )->get_connected( $post->ID );
            while ( $connected->have_posts() ) : $connected->the_post();
                  //do all $connected stuff here
           endwhile;
    
          //now do stuff with original $clients loop
          setup_postdata( current( $clients ) );
          the_title();
    endforeach;
Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘[Plugin: Posts 2 Posts] Can't display connected posts, Am I doing this correct?’ is closed to new replies.