0

I am generating a Featured Content with the following code:

<?php
$featured_posts = get_featured_posts();
foreach ( (array) $featured_posts as $order => $post ) :
    setup_postdata( $post );
    get_template_part( 'content', 'featured-post' );
endforeach;
?>

The page looks like this, sorted by ID:

<article id="post-201">...</article>
<article id="post-204">...</article>
<article id="post-227">...</article>
<article id="post-331">...</article>
<article id="post-530">...</article>
<article id="post-633">...</article>
<article id="post-759">...</article>

I need to sort by Page Order instead. Is this code OK for this purpose?. This works OK.

usort($featured_posts, function($a, $b) {
    return $a->menu_order - $b->menu_order;
});
1
  • 1
    What's that get_featured_posts() function? Never seen it before and can't find it in the WordPress documentation anywhere.
    – YourManDan
    Commented Aug 11, 2023 at 14:19

1 Answer 1

1

If get_featured_posts() returns an array of WP_Post objects, then yes, the usort() function you have should work properly.

If you have access to get_featured_posts(), and it uses get_posts() or a WP_Query instance, then you can set the orderby parameter to menu_order, and drop the usort() call.

Not the answer you're looking for? Browse other questions tagged or ask your own question.