Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

up vote 0 down vote accepted

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.

share|improve this answer
    
Works like a charme, Steven. Thanks! I combine $post and $fields in a new array and off I go ;-) –  Matthias Max Apr 22 at 13:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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