WordPress.org

Ready to get started?Download WordPress

Forums

Posts 2 Posts
How to call two post types connected in one query? (15 posts)

  1. e-duplex
    Member
    Posted 3 years ago #

    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!

  2. scribu
    Member
    Plugin Author

    Posted 3 years ago #

    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.

  3. e-duplex
    Member
    Posted 3 years ago #

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

  4. scribu
    Member
    Plugin Author

    Posted 3 years ago #

    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>";
  5. e-duplex
    Member
    Posted 3 years ago #

    // 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 "
    ";

  6. e-duplex
    Member
    Posted 3 years ago #

    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.

  7. scribu
    Member
    Plugin Author

    Posted 3 years ago #

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

  8. e-duplex
    Member
    Posted 3 years ago #

    Excellent! Your plugin is fantastic and very usefull!

  9. e-duplex
    Member
    Posted 3 years ago #

    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?

  10. scribu
    Member
    Plugin Author

    Posted 3 years ago #

    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?

  11. e-duplex
    Member
    Posted 3 years ago #

    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',
                  ),
                ) );
  12. scribu
    Member
    Plugin Author

    Posted 3 years ago #

    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',
                ) );
  13. jrenzi
    Member
    Posted 3 years ago #

    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

  14. jrenzi
    Member
    Posted 3 years ago #

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

  15. scribu
    Member
    Plugin Author

    Posted 3 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic