Skip Navigation

[Resolved] orderby child post custom field

This support ticket is created 2 years, 11 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 Luo Yang 2 years, 11 months ago.

Assisted by: Luo Yang.

Author
Posts
#2312361

Dear Sir/Madam,

I have a relationship post ebook and chapters, I want to list all the chapters belong to an ebook

        $chapters = toolset_get_related_posts( 
            get_the_ID(), 
        	'ebook-chapter',
	        array(
		        'query_by_role' => 'parent', 
		        'role_to_return' => 'child',
		        'return' => 'post_object',
                'args' => [
                    'meta_key' => 'wpcf-ebook-chapter-order',
                    'meta_value'    => '%',
                    'meta_compare'  => 'LIKE'
                ],
		        'orderby' => 'meta_value_num',
                'order '    => 'ASC',
            ),
        );
        foreach ($chapters as $chapter)  {
            printf("%s - %s<br/>", get_post_meta($chapter->ID, 'wpcf-ebook-chapter-order', true), get_the_title($chapter->ID));
        }

No matter I put ASC or DESC to 'order', the result is still the same, wpcf-ebook-chapter-order is the custom field in child post, I only want to order by the chapter order, is it necessary to have the arguments

                    'meta_value'    => '%',
                    'meta_compare'  => 'LIKE'

Could you please correct me what I missed in code?

#2312793

Hello,

I suggest you try the WordPress PHP class WP_Query like this:

$query = new WP_Query( 
	array(
		'post_type' => 'chapter',
		'posts_per_page' => -1,
		//new toolset_relationships query argument
		'toolset_relationships' => array(
			'role' => 'child',
			'related_to' => get_the_ID(),
			// this will work only with relationships that have existed before the migration
			// if possible, use the relationship slug instead of an array
			'relationship' => 'ebook-chapter'
		),
		'meta_key' => 'wpcf-ebook-chapter-order',
		'orderby' => 'meta_value_num',
		'order' => 'ASC',
	)
);
$chapters = $query->posts;
foreach ($chapters as $chapter)  {
    printf("%s - %s<br/>", get_post_meta($chapter->ID, 'wpcf-ebook-chapter-order', true), get_the_title($chapter->ID));
}

More help:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/how-to-migrate-your-site-to-new-post-relationships/#new-approach
section "NEW APPROACH"

#2312821

Dear Luo Yang,

Thanks for your reply. If I know the chapter post id, how can I query the previous and next post id?

Best regards,

Kelvin.

New threads created by Luo Yang and linked to this one are listed below:

https://toolset.com/forums/topic/how-can-i-query-the-previous-and-next-child-post-id/

#2312869

I assume the original question of this thread is resolved, for other new questions, please check the new thread here:
https://toolset.com/forums/topic/how-can-i-query-the-previous-and-next-child-post-id/