Posts 2 Posts
[resolved] query with multiple post types and same relationship overrides results (3 posts)

  1. aaron@rossano.com.au
    Posted 3 years ago #

    hi, i've created a wp_query to get two custom post types. post_type1 and post_type2.
    both are connected to post_type3. when i set my relationship on the query, the second relationship overrides the first one and erases the results, that forces me to use a different name for the results and having to duplicate code further when looping within the relationship.

    wp_query: 'post_type' => array('post_type1', 'post_type2'),..

    p2p_type( 'post_type1_to_post_type3' )->each_connected( $my_query, array(), 'post3' );
    p2p_type( 'post_type2_to_post_type3' )->each_connected( $my_query, array(), 'post3' );

    am i doing something wrong, or is this a bug?


  2. scribu
    Plugin Author

    Posted 3 years ago #

    That's an interesting edge case, but having each_connected() automatically merge the results doesn't seem like the correct behaviour to me.

    And there's nothing preventing you from merging the two lists yourself:

    p2p_type( 'post_type1_to_post_type3' )->each_connected( $my_query, array(), 'foo' );
    p2p_type( 'post_type2_to_post_type3' )->each_connected( $my_query, array(), 'bar' );
    foreach ( $my_query->posts as $post ) {
      $post->post3 = array_merge( $post->foo, $post->bar );
  3. aaron@rossano.com.au
    Posted 3 years ago #

    that's not a bad idea to set the array manually.
    ideally i think it should do it automatically so that i wouldn't have to worry about losing data while using different post types.
    thanks so much for the quick reply!

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • Posts 2 Posts
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic