I am having a custom block which uses the following code to fetch a list of comma separated posts.
$numberposts = "7,9,5,10";
$post_ids = explode( ',', $numberposts );
$args = array(
'post_type' => 'post',
'post__in' => $post_ids,
'numberposts' => '9999999'
);
$list_posts = get_posts( $args );
The problem is that the returned data is not ordered by the original order of the ids supplied. Is it possible to do that? Can you please help?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
You can set the orderby to post__in. Here is a superior post loop:
$args = [
'post__in' => [ 1, 2, 3, 4 ],
'orderby' => 'post__in',
];
$q = new WP_Query( $args );
if ( $q->have_posts() ) {
while ( $q->have_posts() ) {
$q->the_post();
?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php the_title(); ?>
</article>
<?php
}
wp_reset_postdata();
} else {
echo '<p>No posts found</p>';
}
Important changes:
get_postsdoes not fire all the loop lifecycle filters, bypasses caches by default (suppress_filtersis set to true inget_posts), and doesn’t set the current post for you. It also usesWP_Queryinternally, so I cut out the middleman and usedWP_Querydirectly. This loop is both faster, more expandable, and more compatible- I removed
numberpostsandpost_type, these are unnecessary and would actually slow down the query if WP used them. WP knows which posts you want viapost__inso it’ll just fetch those orderbyis set topost__inso posts will appear in the same order they’re requested- I used a PHP array instead of using
explodeon a string ( which gives the same result so why do the extra work? ) - I used
the_IDandpost_classso the inner loops tags would have the appropriate classes
I strongly recommend you read through this document: https://developer.wordpress.org/reference/classes/wp_query/ it will save a lot of time in the future and has a lot of examples.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0