Support » Plugin: Posts 2 Posts » [Plugin: Posts 2 Posts] Looping the loop without WP_Query?

  • Resolved diniscorreia

    (@diniscorreia)


    Hey there,

    I’m using Posts 2 Posts and the Zoninator plugin (to edit content “zones” on my theme).

    Zoninator works with sort of a custom query (similar to get_posts(), if I understand it correctly):

    $zone_posts = z_get_posts_in_zone( 'homepage', array( 'numberposts' => 1, 'post_type' => 'any' ), false );
    
    foreach( $zone_posts as $post )  {
    	setup_postdata($post);
    	get_template_part( 'content', get_post_format() );
    }

    This returns the posts I have assigned to the ‘homepage’ zone.

    Since there’s no WP_Query going on, is it possible to get the connected posts using Posts 2 Posts?

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

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

    (@scribu)

    With the development version (1.3-alpha3), you can just pass the array of post objects to WP_Query or to get_posts():

    $connected = get_posts( array(
      'connected_items' => $zone_posts,
      'connected_type' => array( 'CONNECTION_TYPE1', 'CONNECTION_TYPE2', ... ),
      'connected_direction' => 'any',
      'post_type' => 'any',
      'suppress_filters' => false
    ) );

    This should return all the connected posts involved.

    Thread Starter diniscorreia

    (@diniscorreia)

    Thanks you so much! That was exactly what I was trying to achieve.

    By the way, can I use $connected on p2p_list_posts?

    Thanks, again.

    EDIT: Hmm just tried it – obviously, if I use p2p_list_posts($connected) on the loop I get the same list of post for every post on the loop :\

    Plugin Author scribu

    (@scribu)

    I just moved the list splitting logic from each_connected() into a standalone function in the development version. Usage example:

    $connected = get_posts( array(
      'connected_items' => $zone_posts,
      'connected_type' => array( 'CONNECTION_TYPE1', 'CONNECTION_TYPE2', ... ),
      'connected_direction' => 'any',
      'post_type' => 'any',
      'suppress_filters' => false
    ) );
    
    p2p_distribute_connected( $zone_posts, $connected, 'connected' );
    
    foreach ( $zone_posts as $post ) {
      setup_postdata($post);
    
      the_title();
    
      p2p_list_posts( $post->connected );
    
      ...
    }
    Thread Starter diniscorreia

    (@diniscorreia)

    Yay, thanks again! Works as expected. *looks for donate button on your site* 🙂

    Another quick question: if I set 'connected_type' => array( 'posts_to_posts', 'posts_to_galleries', 'posts_to_videos') it only lists posts_to_galleries and _to_videos – however, if I only set 'connected_type' => array( 'posts_to_posts') it does lists the connections from that type. Could this be a bug or expected behavior, since it’s an indeterminate connection type?

    Thank you so much again.

    EDIT: Actually, it seems the problem occurs everytime I set more than two connection types on that array.

    Plugin Author scribu

    (@scribu)

    I forgot one thing about the get_posts() call: 'nopaging' => true. See if that helps.

    *looks for donate button on your site*

    If you can’t find it, it means I suck at design. 🙂

    Thread Starter diniscorreia

    (@diniscorreia)

    Ah, that was it!

    By the way, how can that new function still be used in a normal WP Query? (I have some pages on the project that don’t rely on Zoninator).

    Thanks! And just kicking, the button is very easily found 🙂

    Plugin Author scribu

    (@scribu)

    It just takes two arrays of posts:

    p2p_distribute_connected( $my_query->posts, $connected, 'connected' );
    Thread Starter diniscorreia

    (@diniscorreia)

    Hmm I’m using the global $wp_query like this:

    
    $connected = new WP_Query ( array(
      'connected_items' => $wp_query->posts,
      'connected_type' => array( 'posts_to_posts', 'posts_to_fotogalerias', 'posts_to_videos'),
      'connected_direction' => 'any',
      'post_type' => 'any',
      'nopaging' => true,
      'suppress_filters' => false
    ) );
    
    p2p_distribute_connected( $wp_query->posts, $connected, 'connected' );
    

    …but it only lists one connection for one connection type (even with nopagingset) and I get some warnings (Notice: Trying to get property of non-object in [...]/wp-content/plugins/posts-to-posts/core/api.php on line 339).

    Thanks!

    Plugin Author scribu

    (@scribu)

    p2p_distribute_connected( $wp_query->posts, $connected->posts, 'connected' );
    Thread Starter diniscorreia

    (@diniscorreia)

    Ah, brilliant.

    Also, silly me making a new WP_Queryfor $connected – it works if I do it with get_posts.

    Thanks again for all the help, donation on its way.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘[Plugin: Posts 2 Posts] Looping the loop without WP_Query?’ is closed to new replies.