WordPress.org

Ready to get started?Download WordPress

Forums

Posts 2 Posts
posts 2 posts - How to orderby by conneced posts (6 posts)

  1. maorb
    Member
    Posted 2 years ago #

    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/

  2. maorb
    Member
    Posted 2 years ago #

    Any idea how to solve this?
    Thanks

  3. scribu
    Member
    Plugin Author

    Posted 2 years ago #

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

  4. maorb
    Member
    Posted 2 years ago #

    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

  5. Cristiano
    Member
    Posted 1 year ago #

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

    Any help?
    Thanks

  6. Cristiano
    Member
    Posted 1 year ago #

    @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! ;)

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags

No tags yet.