Skip Navigation

[Resolved] toolset_get_related_posts ‘orderby’ date is missing

This thread is resolved. Here is a description of the problem and solution.

Problem:
The user would like to order the results of toolset_get_related_posts by date.

Solution:
I think, that if you put "null" for the "orderby" argument you will have the default order which is by ID. https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

Or you could try with our API for WP_Query class, it would give you more control over the order settings. Check this article https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/how-to-migrate-your-site-to-new-post-relationships/

Relevant Documentation:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/how-to-migrate-your-site-to-new-post-relationships/

This support ticket is created 4 years, 3 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 - - 9:00 – 13:00
14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 - - 14:00 – 18:00

Supporter timezone: Africa/Casablanca (GMT+01:00)

This topic contains 3 replies, has 2 voices.

Last updated by pierreN 4 years, 2 months ago.

Assisted by: Jamal.

Author
Posts
#1829723

Hello,

On the loop that displays related posts, I need to order them by date : newest first, oldest last.
I thought it was a basic query but it seems that 'orderby' => 'date' is missing. Can you tell me how to display the latest post first? Otherwise related posts won't have any sense.

Thank you,
Vinciane

#1829741

If "date" isn't available. Is there a way to order them by post_ID? Biggest number wins 🙂

#1830557

Hello and thank you for contacting the Toolset support.

I think, that if you put "null" for the "orderby" argument you will have the default order which is by ID. https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

Or you could try with our API for WP_Query class, it would give you more control over the order settings. Check this article https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/how-to-migrate-your-site-to-new-post-relationships/

I hope this helps. Let me know if you have any questions.

#1838897

Hello,

Thanks for your answer. We tried to modify our code with the new structure but it echoes the latest 6 posts of "posts" instead of looking into the relationship. Could you tell me if you spot the issue here?


<?php
  //posts by this author
   //global $post;
 //  echo 'ID='. get_the_ID();

   $authr_query = new WP_Query( 
    array(
	'post_type' => 'post',	
     'posts_per_page' => 1,
     //new toolset_relationships query argument
     'toolset_relationships' => array(
      'role' => 'child',
      'related_to' => get_the_ID(),      
      'relationship' => 'post-author',
		 //'relationship' => array( 'article-author', 'post' )
     ),
		'orderby' => 'meta_value',
  		'order' => 'ASC',
     )
   ); 
   
  

   if ( $authr_query->have_posts() ) {
	 while ( $authr_query->have_posts() ) {
        $authr_query->the_post();
		?>      


    
        <h3><?php the_title(); ?></h3>			
		<p class="date"><span class="far fa-calendar-alt"></span><?php the_time('d M Y'); ?></p>
			

    <?php
	 wp_reset_postdata();
	 
	  }
 }
  wp_reset_query();
	   wp_reset_postdata(); ?>