Couple of things – avoid query_posts
, use a new query object or the get_posts
function (read all about it). And the_ID()
echo’s the ID, use get_the_ID()
instead.
However, you can save some memory and processing by simply iterating over an array of posts (i.e. not using a proper “loop” and setting up the global post), and just grab the ID directly:
$product_posts = get_posts(
array(
'posts_per_page' => -1,
'post_type' => 'product',
)
);
$ex_id =
$inc_id = array();
foreach ( $product_posts as $product_post ) {
if ( in_array( 'yes', get_field( 'exclude', $product_post->ID ) ) )
$ex_id[] = $product_post->ID;
elseif ( in_array( 'yes', get_field( 'include', $product_post->ID ) ) )
$inc_id[] = $product_post->ID;
}
Note how I’m passing $product_post-ID
as a second argument to get_field
. This is because by default ACF will use the current “global” post, so we need to tell it which post we actually want to grab data from.