WordPress.org

Support

Support » Plugins and Hacks » [Plugin: Posts 2 Posts] posts 2 posts – How to orderby by conneced posts

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

  • maorb
    Participant

    @maorb

    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/

Viewing 5 replies - 1 through 5 (of 5 total)
  • maorb
    Participant

    @maorb

    Any idea how to solve this?
    Thanks

    Plugin Author scribu
    Member

    @scribu

    You’d need to hook into ‘posts_orderby’ and change the SQL manually.

    maorb
    Participant

    @maorb

    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

    @maorb Could you solve it? It seems so simple, but I can´t find the solution also…

    Any help?
    Thanks

    @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.