Skip Navigation

[Resolved] Post Relationship Order not working

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

Problem:

I want a Custom Post Type Relationship (Lecturer) to load in a particular order: alphabetically ascending by custom field value (lecturer-surname):

https://toolset.com/forums/topic/post-relationship-order-not-working/#post-1215442

Solution:

You can try the WP_Query, like this:

https://toolset.com/forums/topic/post-relationship-order-not-working/#post-1216755

Relevant Documentation:

https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/how-to-migrate-your-site-to-new-post-relationships/#wp_query-argument-for-querying-by-related-posts

This support ticket is created 5 years, 8 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/Hong_Kong (GMT+08:00)

This topic contains 3 replies, has 2 voices.

Last updated by gabrielB 5 years, 8 months ago.

Assisted by: Luo Yang.

Author
Posts
#1215442

I want a Custom Post Type Relationship (Lecturer) to load in a particular order: alphabetically ascending by custom field value (lecturer-surname).

  

    $course_id = get_the_ID();
    $related_lecturers = toolset_get_related_posts(
		$course_id, // get posts related to this one
		'course-lecturer', // relationship between the posts
		'parent', // get posts where $writer is the parent in given relationship
		100, 0, // pagination
		array(
            'post_type' => 'lecturer',
            'orderby' => 'meta_value',
            'meta_key' => 'wpcf-lecturer-surname',
            'order' => 'ASC'
        ), // How was his surname, again…?
		'post_object',
		'child'
	);

Link to a page where the issue can be seen: hidden link

#1216749

Hello,

Thanks for the details, I can login into your website, will update here if there is anything found

#1216755

The function toolset_get_related_posts() is different from WP_Query, the "meta_key" should be put insider into argument "$args".

In your case, you can try the WP_Query, like this:

$course_id = get_the_ID();

$query = new WP_Query( 
	array(
		'post_type' => 'lecturer',
		'numberposts' => -1,
		//new toolset_relationships query argument
		'toolset_relationships' => array(
			'role' => 'child',
			'related_to' => $course_id,
			'relationship' =>'course-lecturer'
		),
		'meta_key' => 'wpcf-lecturer-surname',
		'orderby' => 'meta_value',
		'order' => 'ASC',
	)
);
$related_lecturers = $query->posts;

More help:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/how-to-migrate-your-site-to-new-post-relationships/#wp_query-argument-for-querying-by-related-posts

#1219513

My issue is resolved now. Thanks so much!