Skip to main content
added 147 characters in body
Source Link
Brethlosze
  • 149
  • 1
  • 2
  • 11

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>

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

usort($featured_posts, function($a, $b) {
    return $a->menu_order - $b->menu_order;
});

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>

How can I sort by Order or Date instead?

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;
});
Source Link
Brethlosze
  • 149
  • 1
  • 2
  • 11

Sort Featured Content by Order instead of ID

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>

How can I sort by Order or Date instead?