• First, this plugin is fantastic! Second, the problem. In my project there are two post types connected: post and writers. At home there are nested loops with last 10 posts, each post with its writer. But I wonder if there are too many queries to DB.

    `while (have_posts()) : the_post();
    the_title();

    $the_query = new WP_Query( array(
    ‘post_type’ => ‘writer’,
    ‘connected’ => $post->ID, ) );
    while ($the_query->have_posts()) : $the_query->the_post();
    the_title();
    endwhile;

    endwhile;`

    Is it posible to create only one WP_Query object with last 10 posts with its writers connected for only one loop?

    I hope I make myself clear. Thanks!

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

    (@scribu)

    How about something like this:

    $query = new WP_Query( array(
      'post_type' => 'post',
      'each_connected_to' => array(
        'post_type' => 'writer',
        'orderby' => 'title',
        'order' => 'asc'
      ),
      'each_connected_meta' => array()  // optional
    ) );
    
    while ( $query->have_posts() ) : $query->the_post();
      the_title();
    
      foreach ( $post->connected_to as $p2p_id => $writer ) {
        echo $writer->post_title;
      }
    endwhile;

    As you can see, each $post has a connected_to property, which is an array of connected posts.

    Note that this is not implemented yet; I’m just brainstorming.

    Thread Starter e-duplex

    (@e-duplex)

    It would be very usefull! Do you think you could develope it soon?

    Plugin Author scribu

    (@scribu)

    Try the development version (0.6-alpha).

    So you have 'each_connected', 'each_connected_to' and 'each_connected_from', with matching $post->connected, $post->connected_to and $post->connected_from.

    Also, you can nest it as many levels as you want:

    $posts = get_posts( array(
    	'post_type' => 'post',
    	'nopaging' => true,
    	'each_connected' => array(
    		'post_type' => 'writer',
    		'nopaging' => true,
    		'each_connected' => array(
    			'post_type' => 'genre',
    			'nopaging' => true,
    		),
    	),
    	'suppress_filters' => false
    ) );
    
    echo "<pre>";
    foreach ( $posts as $post ) {
    	echo $post->post_title . "\n";
    
    	foreach ( $post->connected as $writer ) {
    		echo $writer->post_title . "\n";
    
    		foreach ( $writer->connected as $genre ) {
    			echo $genre->post_title . "\n";
    		}
    	}
    }
    echo "</pre>";
    Thread Starter e-duplex

    (@e-duplex)

    // 1. WITH get_posts
    $posts = get_posts( array(
    ‘post_type’ => ‘post’,
    ‘nopaging’ => true,
    ‘orderby’ => ‘ID’,
    ‘each_connected’ => array(
    ‘post_type’ => ‘writer’,
    ‘nopaging’ => true,
    ),
    ‘suppress_filters’ => false
    ) );

    echo “

    ";
                foreach ( $posts as $post ) {
                	echo $post->post_title . "\n";
    
                	foreach ( $post->connected as $writer ) {
                		echo $writer->post_title . "\n";
                	}
                }
                echo "

    “;

    Thread Starter e-duplex

    (@e-duplex)

    Sorry, my last message was an error.

    Hi Scribu! I tried with get_post() and it works fine:

    
                // 1. WITH get_posts
                $posts = get_posts( array(
                	'post_type' => 'post',
                	'nopaging' => true,
                    'orderby' => 'ID',
                	'each_connected' => array(
                		'post_type' => 'writer',
                		'nopaging' => true,
                	),
                	'suppress_filters' => false
                ) );
    
                echo "
    
    ";
                foreach ( $posts as $post ) {
                	echo $post->post_title . "\n";
    
                	foreach ( $post->connected as $writer ) {
                		echo $writer->post_title . "\n";
                	}
                }
                echo "

    ";

    But when I tried with WP_Query some writers didn’t appeared:

    
                // 2. WITH WP_Query
                $query = new WP_Query( array(
                  'post_type' => 'post',
                  'orderby' => 'ID',
                  'order' => 'ASC',
                  'each_connected' => array(
                        'post_type' => 'writer',
                        'order' => 'asc'
                  ),
                ) );
    
                while ( $query->have_posts() ) : $query->the_post();
                  the_title();
    
                  foreach ( $post->connected as $p2p_id => $writer ) {
                    echo $writer->post_title;
                  }
                  echo "<hr/>";
                endwhile;
    

    I don′t understand what happens in my code.

    Plugin Author scribu

    (@scribu)

    When you’re using WP_Query, you forgot to set 'nopaging' => true inside ‘each_connected’.

    Thread Starter e-duplex

    (@e-duplex)

    Excellent! Your plugin is fantastic and very usefull!

    Thread Starter e-duplex

    (@e-duplex)

    Hi scribu! I am using your 0.6 alpha version. It have been doing well until I tried to filter writters by category. Some writters do not appear and some do (without logic). I do not understand what happens…

    
    
    $query = new WP_Query( array(
                  'post_type' => 'post',
                  'nopaging' => true,
                  'orderby' => 'title',
                  'order' => 'ASC',
                  'each_connected' => array(
                        'post_type' => 'writter',
                        'nopaging' => true,
                        'cat' => '3',
                  ),
                ) );

    What do think that it could be happen?

    Plugin Author scribu

    (@scribu)

    Does it work properly if you just do the inner query? How about if you also add a ‘connected’ query var to the inner query?

    Thread Starter e-duplex

    (@e-duplex)

    Sorry scribu! I do not know where I should put the ‘connected’ query var… I tried but it gave me an error message:

    
    
    $query = new WP_Query( array(
                  'post_type' => 'post',
                  'nopaging' => true,
                  'orderby' => 'title',
                  'order' => 'ASC',
                  'connected' => 'any',
                  'each_connected' => array(
                        'post_type' => 'writter',
                        'nopaging' => true,
                        'cat' => '3',
                  ),
                ) );
    Plugin Author scribu

    (@scribu)

    I mean like this:

    $query = new WP_Query( array(
                        'post_type' => 'writter',
                        'nopaging' => true,
                        'cat' => '3',
                ) );

    then, like this:

    $query = new WP_Query( array(
                        'connected' => $some_post_id,
                        'post_type' => 'writter',
                        'nopaging' => true,
                        'cat' => '3',
                ) );

    Hi Scribu,

    I’m having a similar problem, and I’m going nuts here. This should be pretty simple but can’t get it to work.

    I have this setup in my functions.php

    function my_connection_types() {
    	if ( !function_exists( 'p2p_register_connection_type' ) )
    		return;
    
    	p2p_register_connection_type( array(
    		'from' => 'promocion',
    		'to' => 'post'
    	) );
    }
    add_action( 'init', 'my_connection_types', 100 );

    And now I’m trying to get promotions connected to posts that are archived under category ‘4’ for example.

    $promotions = get_posts( array(
                	'post_type' => 'promotion',
                	'nopaging' => true,
                    'orderby' => 'ID',
                	'each_connected' => array(
                		'post_type' => 'post',
                		'nopaging' => true,
                		'cat' => '4'
                	),
                	'suppress_filters' => false
                ) );

    Is this possible? Thanks in advance

    Anyone? I really need to sort this out ASAP. Or at least any idea how to workaround this?

    Plugin Author scribu

    (@scribu)

    It doesn’t work like that.

    What you’re getting are promotions. Then, for each promotion, you’re getting the posts connected to that promotion, which belong in category 4.

    You should probably be doing it the other way around: Get the posts in category 4 and then get the promotions connected to those posts.

    It’s easier to understand if you don’t use each_connected at first, but foreach loops.

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘[Plugin: Posts 2 Posts] How to call two post types connected in one query?’ is closed to new replies.