2

When retrieving posts with wp_query how do I make the loop contain the acf fields?

    $args = array( 'post_type' => $postTypes, 
                   'posts_per_page' => $numPosts, 
                   'offset' => $offset,
                   'meta_query' => $filterArgs );

$loop = new WP_Query( $args );

$postTypes is an array I have filled in advance containing "post", "page" etc. whatever I need.

The loop will contain only the "standard" WP columns like post_content and so on.

1 Answer 1

1

The loop doesn't really contain anything.

It does include methods in order to easily output your data such as the_title(), this is done when calling the_post()

Then, within the loop you'll be able to use get_the_ID() and get_post_meta() to get your custom fields (including ACF ones).

So if you have retrieved your desired posts from that query then you'll be able to do this:

<?php while($loop->have_posts()) : $loop->the_post(); ?>   
   <h2><?php the_title(); ?></h2>  
   <?php the_content(); ?>
   <p>My Custom Field: <?php echo get_post_meta(get_the_ID(),'my-custom-field', true); ?>
<?php endwhile; wp_reset_postdata(); ?>

Hope that helps.

1
  • Works like a charme, Steven. Thanks! I combine $post and $fields in a new array and off I go ;-) Commented Apr 22, 2014 at 13:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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