Is there a way to order children of post?

there is a main post:

$parent = wp_insert_post([
    'post_type' = 'parent',
    'post_name' = 'test parent'
]);

and the children:

$child1 = wp_insert_post([
    'post_type' = 'child',
    'post_name' = 'test child1'
]);
$child2 = wp_insert_post([
    'post_type' = 'child',
    'post_name' = 'test child2'
]);
$child3 = wp_insert_post([
    'post_type' = 'child',
    'post_name' = 'test child3'
]);

lets match them:

wp_update_post(
    array('ID' = $child1, 'post_parent' = $parent)
);
wp_update_post(
    array('ID' = $child2, 'post_parent' = $parent)
);
wp_update_post(
    array('ID' = $child3, 'post_parent' = $parent)
);

finally to list them:

$children = get_children(['post_parent' = $parent]);

this is theoretically seems to be OK, but it only describes the relation itself, not the order. What if I want to order the children? How to do it?

Topic get-children posts Wordpress

Category Web


This should be doable in the get_children() query, try the following:
Take a look at the codex entry of get_children(). You will see that get_children() uses the same args as get_posts(). If you than look at get_posts() you will see that you have 2 order arguments to work with:

'orderby' => 'date',
'order' => 'DESC',

For all possible orderby and order parameters, you can take a look at WP_Query codex.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.