Skip Navigation

[Resuelto] Child posts load in randon order everytime while I want them to be sorted

This support ticket is created hace 5 años, 9 meses. 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.

Hoy no hay técnicos de soporte disponibles en el foro Juego de herramientas. Siéntase libre de enviar sus tiques y les daremos trámite tan pronto como estemos disponibles en línea. Gracias por su comprensión.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Este tema contiene 1 respuesta, tiene 2 mensajes.

Última actualización por Christian Cox hace 5 años, 9 meses.

Asistido por: Christian Cox.

Autor
Mensajes
#1200128

I am trying to:
Load child posts in the order that they show up on the wordpress admin panel.

Link to a page where the issue can be seen:
enlace oculto
Particularly the blue background sections with the title "Salesforce Products we Deliver"

I expected to see:
I expect the products in that section to display in the same order everytime. That order being the order in wich they are displayed in the wordpress dashboard where the list of that post types is

Instead, I got:
The products load in different random positions.

This is the code that I'm using on the theme to get the posts:

<?php
$post_id = get_the_ID(); // parent post ID
$child_posts = toolset_get_related_posts(
$post_id, // get posts related to this one
'industry-products', // relationship slug is identical to repeatable field group slug
'parent', // get posts where $post_id is the parent in given relationship
1000000, 0, // pagination
array('meta_key'=>'toolset-post-sortorder'), // no extra query criteria
'post_object', // return an array of post IDs
'child',// return the child post, which is the RFG in this relationship
'meta_value_num',
'ASC'
);
foreach ($child_posts as $child_post) { ?>

<?php
echo '<img src="' . types_render_field('product-white-icon', array('id'=>$child_post->ID, 'output' => 'raw' )) . '">';
echo '<h4>' . get_the_title($child_post->ID) . '</H4>';
?>

<?php } ?>

#1200218

Hi, the post relationships API "orderby" attribute only accepts these options:
null - No order
title - Title of the post, alphabetical
meta_value - Custom field, alphabetical
meta_value_num - Custom field as a number

The meta key toolset-post-sortorder only applies for Repeatable Field Groups, since they can be manually sorted. Title won't work for your needs either. The only other option then is "null", which means the results will not be sorted. You'll have to sort the results using PHP usort and a custom algorithm. Here's an example that sorts by post date GMT in descending order:

function compare_child_post_dates($a, $b)
{
  $date_a = strtotime($a->post_date_gmt);
  $date_b = strtotime($b->post_date_gmt);
  $res = 0;
  if ( $date_a < $date_b) {
     $res = 1;
  }
  return $res;
}

$post_id = get_the_ID(); // parent post ID
$child_posts = toolset_get_related_posts(
$post_id, // get posts related to this one
'industry-products', // relationship slug is identical to repeatable field group slug
'parent', // get posts where $post_id is the parent in given relationship
1000000, 0, // pagination
array(), // no extra query criteria
'post_object', // return an array of post IDs
'child',// return the child post, which is the RFG in this relationship
null,
'ASC'
);
usort($child_posts, "compare_child_post_dates");

foreach ($child_posts as $child_post) { ?>
... your code continues ...