WordPress.org

Support

Support » Plugins and Hacks » Posts 2 Posts » [Resolved] [Plugin: Posts 2 Posts] Get connected posts of children

[Resolved] [Plugin: Posts 2 Posts] Get connected posts of children

  • In this situation, I am using get_children to grab a post’s child pages and display them in a tabs. In each tab I’m trying to display the connected posts (in this case from a post type named “ship”).

    The code below is pulling up posts from the right posts type, but the posts it grabs are not connected. In fact, it grabs the same posts even if I remove the “connected” parameter completely.
    This might be obvious, but $child_id is the id of the main post’s child:

    $args = array(
    'post_type' => 'ship',
    'connected_to' => $child_id,
    );
    $lastposts = get_posts( $args );
    foreach($lastposts as $post) : setup_postdata($post);
    $permalink_ship = get_permalink( $post->id );
    $shipid = $post->id;
    $meta .= "<a href=\"".$permalink_ship."\">".$post->post_title."</a><br/>";
    endforeach;

    Oh, also this is what I have in my functions.php:

    function my_connection_types() {
        if ( !function_exists('p2p_register_connection_type') )
            return;
        p2p_register_connection_type( 'cruise', 'ship', true );
    }
    add_action('init', 'my_connection_types', 100);

    Any ideas would be greatly appreciated…

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

    @scribu

    You need to also add 'suppress_filters' => false:

    $args = array(
      'post_type' => 'ship',
      'connected_to' => $child_id,
      'suppress_filters' => false
    );
    $lastposts = get_posts( $args );

    Thanks for the reply scribu… Sorry for my delay in replying.

    I added that change but it’s still behaving in exactly the same way; it pulls posts from the correct post type that I have connected, but they seem arbitrary. The posts that are displayed as “connected” are the same on every post (it’s always 5 posts as well).

    Do you think using WP_query would help?

    I tried using WP_query, but now it pulls ALL posts of post-type ‘ships’:

    $args = array(
    'post_type' => 'ship',
    'connected' => $test_id,
    );
    $my_query = new WP_Query( $args );
    while ( $my_query->have_posts() ) : $my_query->the_post();
            $permalink_ship = get_permalink( $post->id );
    	$ship_title = $post->post_title;
    	$meta .= "<a>".$ship_title."</a>";
    endwhile;

    Plugin Author scribu

    @scribu

    Just before that block of code, write var_dump($test_id); just to make sure it’s not blank.

    I went back to the get_posts method; I see a stupid error was the problem. I had $post->id, lowercase id, so that seemed to be the error.

    However I have a new problem, even though the plugin seems to be working fine, pulling the related posts. After I echo the related posts, the code is set to return to the original post, which it seems to do fine for the rest of the meta-data (which is correctly pulled from the original post, not the related one).
    But when it comes to the_content() it suddenly reverts BACK to the connected post. This is very strange, since doing something like this returns the content of the connected post, but returns the correct ID of the original post: the_content(); echo $post->ID;

    This is the code that works for the related posts:

    $test_id = $post->ID;
    global $post;
    $tmp_post = $post;
    $args = array(
    'post_type' => 'ship',
    'connected' => $test_id,
    'suppress_filters' => false
    );
    $lastposts = get_posts( $args );
            foreach($lastposts as $lastpost) : setup_postdata($lastpost);
    	$permalink_ship = get_permalink( $lastpost->ID );
    	$ship_title = $lastpost->post_title;
    	$meta .= "<a href=\"".$permalink_ship."\">".$ship_title."</a><br />";
    endforeach;
    $post = $tmp_post;

    Any ideas? This seems like a very weird problem.

    Plugin Author scribu

    @scribu

    Instead of $post = $tmp_post;, just call wp_reset_postdata().

    That worked great!
    Thanks again for the help and this essential plugin!

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘[Resolved] [Plugin: Posts 2 Posts] Get connected posts of children’ is closed to new replies.
Skip to toolbar