WordPress.org

Ready to get started?Download WordPress

Forums

Posts 2 Posts
[resolved] Can't display connected posts, Am I doing this correct? (12 posts)

  1. cw17s0n
    Member
    Posted 2 years ago #

    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/

  2. scribu
    Member
    Plugin Author

    Posted 2 years ago #

    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.

  3. cw17s0n
    Member
    Posted 2 years ago #

    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.

  4. scribu
    Member
    Plugin Author

    Posted 2 years ago #

    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.

  5. cw17s0n
    Member
    Posted 2 years ago #

    Thanks, makes sense now.

  6. watermelonkid
    Member
    Posted 2 years ago #

    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?

  7. Manuel Schmalstieg
    Member
    Posted 2 years ago #

    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.

  8. watermelonkid
    Member
    Posted 2 years ago #

    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>

  9. scribu
    Member
    Plugin Author

    Posted 2 years ago #

  10. watermelonkid
    Member
    Posted 2 years ago #

    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?

  11. scribu
    Member
    Plugin Author

    Posted 2 years ago #

    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.

  12. watermelonkid
    Member
    Posted 2 years ago #

    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;

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic