I am using ACF and I am trying to do the following on a page

  1. Create an array of the custom post type "Services" post IDs
  2. Check if the array has at least 1 post ID
  3. If so, for each post ID, display: <?php the_field('the_field','POST_ID');?>
  4. If not, echo "There are no services"

You can use the fields parameter of WP_Query to get just IDs back, then iterate over them with a foreach loop:

$args = array(
    'post_type' => 'services',
    'posts_per_page' => -1,
    'fields' => 'ids',
$query = new WP_Query( $args );

if( $query->have_posts() ){
    foreach( $query->posts as $id ){
        the_field( 'the_field', $id );
} else {
    echo 'There are no services';

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.