Title: [Plugin: Posts 2 Posts] Get connected posts by status?
Last modified: August 20, 2016

---

# [Plugin: Posts 2 Posts] Get connected posts by status?

 *  Resolved [Patrick Daly](https://wordpress.org/support/users/developdaly/)
 * (@developdaly)
 * [14 years, 1 month ago](https://wordpress.org/support/topic/plugin-posts-2-posts-get-connected-posts-by-status/)
 * Is there any way to check the status of the connected post and query by the status?
   I added my own line below, “connected_status”, as an example.
 *     ```
       $connected = new WP_Query( array(
            'connected_type' => 'my_connection_name',
            'connected_items' => 'any',
            'connected_direction' => 'from',
            'connected_status' => array( 'private', 'draft' ),
            'post_type' => 'my_post_type'
       ));
       ```
   
 * [http://wordpress.org/extend/plugins/posts-to-posts/](http://wordpress.org/extend/plugins/posts-to-posts/)

Viewing 7 replies - 1 through 7 (of 7 total)

 *  Plugin Author [scribu](https://wordpress.org/support/users/scribu/)
 * (@scribu)
 * [14 years, 1 month ago](https://wordpress.org/support/topic/plugin-posts-2-posts-get-connected-posts-by-status/#post-2664569)
 * I’m not sure what ‘connected_status’ would do. Do you mean get the posts grouped
   by status, as an associative array?
 *  Thread Starter [Patrick Daly](https://wordpress.org/support/users/developdaly/)
 * (@developdaly)
 * [14 years, 1 month ago](https://wordpress.org/support/topic/plugin-posts-2-posts-get-connected-posts-by-status/#post-2664577)
 * Not grouped, but just like any other WP query using ‘post_status’.
 *     ```
       $connected = new WP_Query( array(
            'connected_type' => 'my_connection_name',
            'connected_items' => 'any',
            'connected_direction' => 'from',
            'post_type' => 'my_post_type'
       ));
       ```
   
 * If I used ‘post_status’ in that query it would get the status for ‘my_post_type’
   posts, but I want to get the status of the connected posts and only show the ‘
   private’ posts.
 *  Plugin Author [scribu](https://wordpress.org/support/users/scribu/)
 * (@scribu)
 * [14 years, 1 month ago](https://wordpress.org/support/topic/plugin-posts-2-posts-get-connected-posts-by-status/#post-2664635)
 * So, if you had posts and pages, you want to get the pages that are connected 
   to private posts, yeah?
 * This is not currently possible without diving into the SQL. Could you give the
   me the actual use-case for this?
 *  Plugin Author [scribu](https://wordpress.org/support/users/scribu/)
 * (@scribu)
 * [14 years, 1 month ago](https://wordpress.org/support/topic/plugin-posts-2-posts-get-connected-posts-by-status/#post-2664638)
 * You can do it like this:
 *     ```
       $connected_posts = get_posts( array(
            'post_status' => 'private',
            'connected_type' => 'posts_to_pages',
            'connected_items' => 'any',
            'connected_direction' => 'from',
            'suppress_filters' => false
       ) );
   
       $connected_pages = get_posts( array(
            'post_type' => 'page',
            'post__in' => wp_list_pluck( $connected_posts, 'p2p_to' ),
       ) );
       ```
   
 * And I think that’s good enough. You have the flexibility to use all the query
   vars that WP_Query accepts.
 * Depending on your connection type, you might have to replace ‘p2p_to’ with ‘p2p_from’.
 * Obviously, you can use WP_Query instead of get_posts() if you need to.
 *  Thread Starter [Patrick Daly](https://wordpress.org/support/users/developdaly/)
 * (@developdaly)
 * [14 years, 1 month ago](https://wordpress.org/support/topic/plugin-posts-2-posts-get-connected-posts-by-status/#post-2664651)
 * Yeah, you understand it correctly.
 * My site (guessages.com) uses 2 custom post types, “guess” and “photo”. Users 
   guess the age of photos that other users have submitted. Your plugin connects
   the guesses to the photos.
 * Every photo and every guess submitted I create as private posts, so they’re only
   available to the logged in user unless I override that.
 * When a user submits their photos they can choose to make it active, putting it
   in the pool of photos to be guessed by everyone, but anytime they want to revert
   and make it inaccessible I have to change the post status. As the plugin stands,
   I can’t differentiate between whether to display a photo in the main pool or 
   not (without using meta or custom SQL, of course).
 * I guess the tricky thing for you to decide is if you want to add something like
   this and where to draw the line. You could create almost every parameter WP_Query
   offers prefaced by “connected_”. I could also stand to benefit from “connected_author”.
   When querying for guesses that the current logged in user made I have to use 
   meta, like so:
 *     ```
       'connected_meta' => array(
            array(
                 'key' => 'guesser_id',
                 'value' => $current_user->ID,
                 'compare' => '=',
                 'type' => 'numeric'
       	)
       )
       ```
   
 * … it would be much easier to use this:
 * `'connected_author' => $current_user->ID`
 *  Thread Starter [Patrick Daly](https://wordpress.org/support/users/developdaly/)
 * (@developdaly)
 * [14 years, 1 month ago](https://wordpress.org/support/topic/plugin-posts-2-posts-get-connected-posts-by-status/#post-2664654)
 * I didn’t see your last reply before I submitted. I think your method might work,
   so let me try it out.
 *  Thread Starter [Patrick Daly](https://wordpress.org/support/users/developdaly/)
 * (@developdaly)
 * [14 years, 1 month ago](https://wordpress.org/support/topic/plugin-posts-2-posts-get-connected-posts-by-status/#post-2664704)
 * `wp_list_pluck` wasn’t working for me, but I ended up using this method.
 * In English, first I query for all the photos that the current users has guessed.
   I pull out all of the ‘p2p_to’ IDs pointing at guess ID. Then, I query for all
   guesses, excluding all the IDs from the previous query.
 * Thanks for your help, man.
 * _[33 lines of code moderated as per the [Forum Rules](http://codex.wordpress.org/Forum_Welcome#Posting_Code).
   The maximum number of lines of code that you can post in these forums is **ten
   lines**. Please use the [pastebin](http://wordpress.pastebin.com/)]_

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘[Plugin: Posts 2 Posts] Get connected posts by status?’ is closed to 
new replies.

 * ![](https://s.w.org/plugins/geopattern-icon/posts-to-posts_7a8e9d.svg)
 * [Posts 2 Posts](https://wordpress.org/plugins/posts-to-posts/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/posts-to-posts/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/posts-to-posts/)
 * [Active Topics](https://wordpress.org/support/plugin/posts-to-posts/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/posts-to-posts/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/posts-to-posts/reviews/)

 * 7 replies
 * 2 participants
 * Last reply from: [Patrick Daly](https://wordpress.org/support/users/developdaly/)
 * Last activity: [14 years, 1 month ago](https://wordpress.org/support/topic/plugin-posts-2-posts-get-connected-posts-by-status/#post-2664704)
 * Status: resolved