Order ‘post__in’ IDs by Array Index in WordPress Query

May 11th, 2020

I use ACF a lot in my projects and when I use the Post Object field to select multiple posts for use with a custom query loop, I always forget how to order based on the posts ordering in the backend.

If you want to mirror the ordering position, adding in the ‘orderby’ => ‘post__in’ to your WP_Query.

$ids = get_field('ids'); 
$args = array (
    'posts_per_page' => -1,
    'post__in' => $ids,
    'orderby' => 'post__in' 
);

This will return your $ids array; ‘first’, ‘second’, ‘third’.

No Comments

Leave a Reply