Title: [Plugin: Posts 2 Posts] posts 2 posts &#8211; How to orderby by conneced posts
Last modified: August 20, 2016

---

# [Plugin: Posts 2 Posts] posts 2 posts – How to orderby by conneced posts

 *  [maorb](https://wordpress.org/support/users/maorb/)
 * (@maorb)
 * [13 years, 9 months ago](https://wordpress.org/support/topic/plugin-posts-2-posts-posts-2-posts-how-to-orderby-by-conneced-posts/)
 * First, many thanks to Scribu – you rock!! What a great plugin is this.
 * I’m trying to order posts by date of all the connected posts, but can’t do it,
   I think I miss a parameter.
    I have 2 cpts – event and teacher that are connected.
   In the single-teacher.php template I want to show all the events of a techer,
   ordered by the most upcoming course to the most far away. I try this –
 *     ```
       p2p_type( 'event_teacher' )->each_connected( $wp_query );
       $connected_events = new WP_Query( array(
         		'post_type' => 'event',
       		'connected_type' => 'event_teacher',
         		'connected_items' => get_queried_object(),
         		'nopaging' => true,
       		'orderby' => 'date',
       	  	'order' => 'ASC',
       		'posts_per_page' => -1
       	) );
       ```
   
 * This gives the correct results, but all events are ordered by date DESC not ASC.
 * I tried also this –
 *     ```
       p2p_type( 'event_teacher' )->each_connected( $wp_query );
       $connected_events = new WP_Query( array(
         		'post_type' => 'event',
       		'connected_type' => 'event_teacher',
         		'connected_items' => get_queried_object(),
         		'nopaging' => true,
       		'posts_per_page' => -1,
       	        'connected_orderby' => 'date',
         		'connected_order' => 'ASC'
       	) );
       ```
   
 * but still nothing changed.
    How could I orderby date ASC (or any other orderby)
   to the connected items fetched?
 * Thanks ahead for any help
 * [http://wordpress.org/extend/plugins/posts-to-posts/](http://wordpress.org/extend/plugins/posts-to-posts/)

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

 *  Thread Starter [maorb](https://wordpress.org/support/users/maorb/)
 * (@maorb)
 * [13 years, 9 months ago](https://wordpress.org/support/topic/plugin-posts-2-posts-posts-2-posts-how-to-orderby-by-conneced-posts/#post-3033063)
 * Any idea how to solve this?
    Thanks
 *  Plugin Author [scribu](https://wordpress.org/support/users/scribu/)
 * (@scribu)
 * [13 years, 9 months ago](https://wordpress.org/support/topic/plugin-posts-2-posts-posts-2-posts-how-to-orderby-by-conneced-posts/#post-3033064)
 * You’d need to hook into ‘posts_orderby’ and change the SQL manually.
 *  Thread Starter [maorb](https://wordpress.org/support/users/maorb/)
 * (@maorb)
 * [13 years, 9 months ago](https://wordpress.org/support/topic/plugin-posts-2-posts-posts-2-posts-how-to-orderby-by-conneced-posts/#post-3033065)
 * Why is the regular parameter for orderby won’t work in this case?
 *     ```
       'orderby' => 'date',
        'order' => 'ASC',
       ```
   
 * Would you please suggest a code snippet to add for hooking into ‘posts_orderby’?
 * Thanks
 *  [Cristiano](https://wordpress.org/support/users/iclapton/)
 * (@iclapton)
 * [13 years, 8 months ago](https://wordpress.org/support/topic/plugin-posts-2-posts-posts-2-posts-how-to-orderby-by-conneced-posts/#post-3033102)
 * [@maorb](https://wordpress.org/support/users/maorb/) Could you solve it? It seems
   so simple, but I can´t find the solution also…
 * Any help?
    Thanks
 *  [Cristiano](https://wordpress.org/support/users/iclapton/)
 * (@iclapton)
 * [13 years, 8 months ago](https://wordpress.org/support/topic/plugin-posts-2-posts-posts-2-posts-how-to-orderby-by-conneced-posts/#post-3033103)
 * [@maorb](https://wordpress.org/support/users/maorb/) I finally found the code
   snippet for hooking it:
 *     ```
       add_filter('posts_orderby', 'date_asc' );
       function date_asc( $orderby )
       {
         global $connected_events;
   
         if(is_singular('teacher')) {
            return "post_date ASC";
         }
   
         return $orderby;
       }
       ```
   
 * Try it! 😉

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

The topic ‘[Plugin: Posts 2 Posts] posts 2 posts – How to orderby by conneced posts’
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/)

 * 5 replies
 * 3 participants
 * Last reply from: [Cristiano](https://wordpress.org/support/users/iclapton/)
 * Last activity: [13 years, 8 months ago](https://wordpress.org/support/topic/plugin-posts-2-posts-posts-2-posts-how-to-orderby-by-conneced-posts/#post-3033103)
 * Status: not resolved