Title: [Plugin: Posts 2 Posts] Get Connected Ancestors?
Last modified: August 20, 2016

---

# [Plugin: Posts 2 Posts] Get Connected Ancestors?

 *  [Mitsugari Yamamoto](https://wordpress.org/support/users/mityama/)
 * (@mityama)
 * [14 years, 6 months ago](https://wordpress.org/support/topic/plugin-posts-2-posts-get-connected-ancestors/)
 * Hey scribu, would it be possible to implement functionality to get the whole 
   tree of connected posts instead of just the directly connected posts?
 * Take this as an example, each of the below are post types, arrows mean Posts 
   2 Posts relations:
    TV Series <- Seasons <- Episodes <- Actors
 * Now, I’d like to retrieve all actors who played in a particular Season or all
   who ever played in the whole Series. I could of course nest several loops to 
   retrieve all Seasons for a Series, then all Episodes for each Season and so on.
   But this would be very slow compared to doing this with a single SQL statement
   and a few JOINs.
 * [http://wordpress.org/extend/plugins/posts-to-posts/](http://wordpress.org/extend/plugins/posts-to-posts/)

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

 *  Plugin Author [scribu](https://wordpress.org/support/users/scribu/)
 * (@scribu)
 * [14 years, 6 months ago](https://wordpress.org/support/topic/plugin-posts-2-posts-get-connected-ancestors/#post-2426814)
 * Technically speaking, this is not a tree, but a directed graph. And those are
   not “ancestors”. An ancestor is a post that has it’s ID as the value of another’s
   post_parent column.
 * I agree that in some cases it would probably be faster to use a single SQL statement,
   but I’m having a hard time imagining an intuitive API for it.
 *  Plugin Author [scribu](https://wordpress.org/support/users/scribu/)
 * (@scribu)
 * [14 years, 6 months ago](https://wordpress.org/support/topic/plugin-posts-2-posts-get-connected-ancestors/#post-2426817)
 * Maybe something like this:
 *     ```
       $query = new WP_Query( array(
         'connected_chain' => array( 'series_to_seasons', 'seasons_to_episodes', 'episodes_to_actors' )
         'connected_items' => $serie_id
       ) );
       ```
   
 * Each element in the `'connected_chain'` array would be a connection type.
 *  Thread Starter [Mitsugari Yamamoto](https://wordpress.org/support/users/mityama/)
 * (@mityama)
 * [14 years, 6 months ago](https://wordpress.org/support/topic/plugin-posts-2-posts-get-connected-ancestors/#post-2426820)
 * Thanks for a once again very quick reply, scribu! Of course you’re right about
   the terminology.
 * That initial draft using “connection chains” looks pretty good. Here’s another
   thought: Would it be possible to simply add a single new parameter like ‘include_indirect_connections
   =true’ to the current API? So you only need this param plus “from” and “to”? 
   I don’t know the inner workings of the plugin, but if technically possible, this
   way seems very intuitive.
 * Of course it would also be nice to (optionally) be able to also specify some “
   exclude” filters, so posts connected via a certain post type would be excluded.
   🙂
 *  Plugin Author [scribu](https://wordpress.org/support/users/scribu/)
 * (@scribu)
 * [14 years, 6 months ago](https://wordpress.org/support/topic/plugin-posts-2-posts-get-connected-ancestors/#post-2426829)
 * > Would it be possible to simply add a single new parameter like ‘include_indirect_connections
   > =true’ to the current API?
 * It would be ambiguous. What if you have several connection types containing the‘
   episode’ post type? I think passing the connection types explicitly is the sweet
   spot between ease of use and preciseness.
 * You’re free to implement `'include_indirect_connections=true'` yourself using
   the ‘parse_query’ hook, as a shortcut which expands to a commonly used chain.
 * > Of course it would also be nice to (optionally) be able to also specify some“
   > exclude” filters, so posts connected via a certain post type would be excluded.
   > 🙂
 * If you define your connection types appropriately, you shouldn’t need this. Plus,
   you have the ‘p2p_connected_args’ hook for further filtering.

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

The topic ‘[Plugin: Posts 2 Posts] Get Connected Ancestors?’ 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/)

 * 4 replies
 * 2 participants
 * Last reply from: [scribu](https://wordpress.org/support/users/scribu/)
 * Last activity: [14 years, 6 months ago](https://wordpress.org/support/topic/plugin-posts-2-posts-get-connected-ancestors/#post-2426829)
 * Status: not resolved