Support

Account

Home Forums Front-end Issues Post Object field with multiple posts into a WP_Query or while loop? Reply To: Post Object field with multiple posts into a WP_Query or while loop?

  • Hello…

    With WP_Query you can use the argument post__in to pass in an array of post IDs…
    See here: https://codex.wordpress.org/Class_Reference/WP_Query#Post_.26_Page_Parameters

    So… with a Post Object ACF filed, you might be returning IDs or Post Objects (default).

    If returning Post Objects, you can use this code to get an array of IDs:

    
    $post_objects = get_field('po');
    $post_object_ids = array_map(function($o) { return $o->ID; }, $post_objects); // array of IDs to pass to: post__in in a WP_Query
    

    Careful though about passing an empty array to post__in because it will return all results.