Hello
I've written some custom code to bring in a repeatable field group. The code works, but it does not respect the order of the puppies added to this repeatable field group (set in the edit post view).
Example link: hidden link (scroll down to "And Their Puppies!")
I've included a screenshot from the "edit post" view and below is the code I'm using.
if(is_singular('litters')){
$litter_id = get_the_ID();
$puppies_in_litter_output = '';
$puppies_in_litter = toolset_get_related_posts( $litter_id, 'litter-puppies', array( 'query_by_role' => 'parent', 'return' => 'post_object' ) );
foreach ($puppies_in_litter as $puppy){
$show_hide_puppy = get_post_meta($puppy->ID, 'wpcf-litter-puppy-show-hide', true);
if($show_hide_puppy =='show'){
$puppy_full_name = get_post_meta($puppy->ID, 'wpcf-litter-puppy-name', true);
$puppy_nick_name = get_post_meta($puppy->ID, 'wpcf-litter-puppy-nickname', true);
$puppy_description = get_post_meta($puppy->ID, 'wpcf-litter-puppy-description', true);
$puppy_image_slug = get_post_meta($puppy->ID, 'wpcf-litter-puppy-image', true);
$puppy_status = get_post_meta($puppy->ID, 'wpcf-litter-puppy-status', true);
$shortcode = str_replace('%1$s', $puppy->ID, '[types field="litter-puppy-image" item="%1$s" size="medium" alt="%%ALT%%" output="html"]');
$thumbnail = do_shortcode($shortcode);
$puppies_in_litter_output .= sprintf('<div class="et_pb_module et_pb_blurb puppy-item %5$s"><div class="et_pb_blurb_content"><div class="ribbon ribbon-top-left"><span>ribbon</span></div><h3 class="et_pb_module_header nickname">%2$s</h3><div class="et_pb_main_blurb_image"><span class="et_pb_image_wrap">%4$s</span></div><div class="et_pb_blurb_container"><div class="et_pb_blurb_description"><div class="full-name">%1$s</div><div class="description">%3$s</div></div></div></div></div>', $puppy_full_name, $puppy_nick_name, $puppy_description, $thumbnail, $puppy_status);
}
}
if($puppies_in_litter_output!==''){
echo sprintf('<div class="puppy-container" data-lazy="false">%1$s</div>', $puppies_in_litter_output);
}
if($puppies_in_litter_output ==''){
echo sprintf('<div class="et_pb_module et_pb_text et_pb_text_align_left et_pb_bg_layout_light"><div class="et_pb_text_inner"><p class="text-center">Photos and Profiles Coming Soon!</p></div></div>');
}
}
Is this something you can help me with?
Thank you
Jules
Hello,
Toolset repeatable field group is using a custom field to store the sortby value, you can follow our document to setup the "orderby" parameter
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts
'rfg_order' is applicable only for repeatable field groups and it means the order which has been set manually by the user
Thank you, Luo. That was exactly what I needed.
For those interested in the answer, all I did was update line 4 to:
$litter_updates = toolset_get_related_posts( $litter_id, 'litter-update', array( 'query_by_role' => 'parent', 'return' => 'post_object', 'orderby' => 'rfg_order' ) );
Cheers!
Jules