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?
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"
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.
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/