Skip Navigation

[Resolved] toolset_get_related_posts 'orderby' date is missing NOT SOLVED

This support ticket is created 4 years, 2 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: Asia/Karachi (GMT+05:00)

This topic contains 12 replies, has 3 voices.

Last updated by pierreN 4 years, 2 months ago.

Assisted by: Waqar.

Author
Posts
#1839687

Hello,

I must open a new ticket because my previous one was marked as resolved but it's not. And it's really urgent now.

We based our code on your answer:
" 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/"

Tested multiple ways including API and couldn't get any result. Sometimes it even echoes the latest posts no matter the relationship.

<?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(); ?>
#1840955

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

The code you are using above looks like it is just copied and pasted from the examples in the documentation without being updated for your site, and the orderby argument hasn't been changed to order by date, so I wouldn't expect it to give you meaningful results.

To be able to help you with the code, can you clarify what are the post types in the relationship, what kind of relationship is it, what is the slug of the relationship, and what kind of posts are you trying to display (i.e. which of the post types is the related post type you want to output)?

#1841463

Probably this would help you to understand my issue better
hidden link
hidden link
No matter what id i use here, it always shows the same result.  You can try with any id and see it showing the same result.
I have displayed the query on the page, so that you get an idea of the issues I am facing.

if(isset($_GET['search_id'])){
	$search_id=sanitize_text_field($_GET['search_id']);
	}else{
}

$qry=array(
	'post_type' => 'post',	
	 'posts_per_page' => 1,	 
	 'ignore_custom_sort' => TRUE,    
     'toolset_relationships' => array(
      'role' => 'child',
      'related_to' => $search_id,      
      'relationship' => 'post-author',	  
	 ));
//print_r($qry);
$the_query = new WP_Query($qry); 

In your previous reply you mentioned above "looks like it is just copied and pasted from the examples".
Of Course it's the same. Shouldn't the parameters be the same?. The parameter values are different in my case.
For example you can see the relationship slug is different

#1841765

Hi,

Thanks for writing back.

In order to troubleshoot this on my test website and suggest the best way forward, I'll need to see exactly how these post types and the relationship are setup in the admin area. Please also share some information about how and where are you using this code snippet.

Note: The two links from your last message are showing "404 not found" page.

I've set your next reply as private and and please make a complete backup copy, before sharing the temporary admin access details.

regards,
Waqar

#1843133

Hello,
Our developper gave you the access to WP admin yesterday. Could you tell me if you found anything? Do you need anything from our side?
I'm sorry but it's really urgent. We were supposed to launch the website 4 weeks ago and we are stuck because of this ordering. It's really important.

Best,
Vinciane

#1843431

Hi Vinciane,

Thank you for sharing the admin access.

I'm currently performing some tests on my website with a similar setup and code will be able to share my findings within next few hours.

Thank you for your patience.

regards,
Waqar

#1844073

Thank you for waiting.

During testing on my website, I was able to show the posts related to the author ID available in the "search_id" URL parameter, using this code:


<?php 
/* Template name: Test*/
get_header(); ?> 

<?php
if(isset($_GET['search_id'])){
	$search_id=sanitize_text_field($_GET['search_id']);
	}else{
}

$qry=array(
	'post_type' => 'post',
	'posts_per_page' => -1,
	'ignore_custom_sort' => TRUE,
	'toolset_relationships' => array(
		'role' => 'parent',
		'related_to' => $search_id,
		'relationship' => 'post-author'
	),
	'orderby' => 'post_date',
	'order' => 'DESC',
);
//print_r($qry);
$the_query = new WP_Query($qry); 
echo $the_query->request;

if ( $the_query->have_posts() ) {
	while ( $the_query->have_posts() ) {
		$the_query->the_post();	
echo '<div>title= ';
the_title();
echo ' DATE= ';
the_time('d M Y');
echo '<hr>';
echo '</div>';
	}

}
?>
<?php get_footer(); ?> 

Notes:

1. I've changed "posts_per_page" to "-1" as I needed all the related posts and not just 1.
2. I've changed "role" to "parent" as posts is the parent in the relationship.
3. The parameters "orderby" and "order" are optional in this case, since by default results are ordered by date in descending order.
( ref: https://developer.wordpress.org/reference/classes/wp_query/#order-orderby-parameters )
4. On your website, the "Post Types Order" plugins is active too. Plugins like these can override the posts order in WordPress query results globally. If you notice incorrect ordering, test with all extra plugins disabled.

#1845255

I have tested with your code. But i always shows the same posts no matter what post id i use.

#1845471

Thanks for writing back.

If the issue still persists, with all extra plugins disabled and a default theme like Twenty Twenty, I'll need a clone/snapshot of the website, so that this can be investigated on a different server.
( ref: https://toolset.com/faq/provide-supporters-copy-site/ )

Note: I've set your next reply as private.

#1845817

Thank you for sharing the duplicator package.

During troubleshooting, I noticed that there were about 6 posts that were set as "sticky" and by default, the WordPress Query class appends the sticky posts at the start of the resutls.
( ref: https://developer.wordpress.org/reference/classes/wp_query/ )

In your query arguments, you can set the "ignore_sticky_posts" to true, to ignore the sticky posts:


$qry=array(
    'post_type' => 'post',
    'posts_per_page' => -1,
    'ignore_custom_sort' => TRUE,
    'toolset_relationships' => array(
        'role' => 'parent',
        'related_to' => $search_id,
        'relationship' => 'post-author'
    ),
    'orderby' => 'post_date',
    'order' => 'DESC',
    'ignore_sticky_posts' => 'true',
);

This should do the trick.

#1846415

I am not getting any result with that query. Have you tried this code on our website?. If so, please share the url to that page.

#1846899

I've checked your template file and noticed that the "role" was still set to "child".

When I changed it to "parent", the query started working, as expected.

Example:

Front-end:
yourwebsite.com/test-to-del/?search_id=5359
( screenshot: hidden link )

Author "Danuta Maria Hübner" ( ID:5359 ) with only one related post:
yourwebsite.com/wp-admin/post.php?post=5359&action=edit
( screenshot: hidden link )

#1850623

After your intervention on the code, my issue is resolved now. Thank you!