WordPress.org

Ready to get started?Download WordPress

Forums

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

  1. aaron@rossano.com.au
    Member
    Posted 1 year 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?

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

  2. scribu
    Member
    Plugin Author

    Posted 1 year 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
    Member
    Posted 1 year 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

About this Topic