Posts 2 Posts: Version 0.6

Nested Queries

If, for each post in a loop, you find yourself doing a subquery using the ‘connected’ query vars, I’ve got good news for you.

Replace something like this:

while ( have_posts() ) : the_post();
	$connected_writers = get_posts( array(
		'post_type' => 'writer',
		'nopaging' => true,
		'connected_to' => $post->ID,
		'suppress_filters' => false
	) );

	foreach ( $connected_writers as $writer ) {
		echo $writer->post_title;
	}
endwhile;

with this:

p2p_each_connected( 'to', 'writers', array(
	'post_type' => 'writer',
	'nopaging' => true,
) );

while ( have_posts() ) : the_post();
	foreach ( $post->connected_writers as $writer ) {
		echo $writer->post_title;
	}
endwhile;

Notice how each $post now has a connected_writers propery, which is an array containing the connected posts.

The main advantage of this method is that it reduces the number of SQL queries dramatically.

Here’s the original forum thread that spurred this feature.

Several bug fixes also went into this release:

In the next version, I will be focusing on improving the default admin UI.

comments powered by Disqus