Home › Forums › General Issues › How to also get ACF fields from a get_post loop
I have the following code to get posts, which are part of a specific category. All these posts have the same ACF fields on them too…
//BUILD THE LOOP AND LOAD THE POSTS
function ithub_exec_summary_landing_loop() {
//DEFINE OUTPUT VAR
$out = '';
//LOOP - GET AL EXECUTIVE SUMMARIES VIA GET_POSTS()
$args = array(
'numberposts' => -1,
'orderby' => 'modified',
'order' => 'ASC',
'category_name' => 'executive-summary'
);
$mExecSumPosts = get_posts($args);
//REFERENCE LOOP FOR PREVIEWING REVISIONS
if ($mExecSumPosts) {
foreach ($mExecSumPosts as $post):
setup_postdata($post);
//DEFINE LOOP VARS
$mtitle = $post->post_title;
$mpublisheddate = get_the_time('Y-m-d', $post->ID);
$mformateddate = date("l F jS, Y", strtotime($mpublisheddate));
//OUTPUT DATA
$out.= "<div class='row'>";
$out.= "<div class='col'>";
$out.= "<h3><a href='{$post->post_name}'>{$mtitle}</a> <span class='h5 text-secondary'>: Status Date of {$mpublisheddate}</span></h3>";
$out.= "</div>";
$out.= "</div>";
endforeach;
wp_reset_postdata();
}
return $out;
}
…But how do not only get the basic tags of each post from this loop, but also the ACF custom fields found on them? For example, each of these posts has an ACF field called ‘product_owner’, which is a ‘select’ type. I want to display my mutli-select values of product_owner, into this get_post loop. Where do I start?
I was able to upgrade the logic of my loop to be manipulated by ACF fields (yeaahh!!!), for example, I can filter the get_post loop by my meta_query of ACF data as shown below…
//BUILD THE LOOP AND LOAD THE POSTS
function ithub_exec_summary_landing_loop() {
//DEFINE OUTPUT VAR
$out = '';
//LOOP - GET AL EXECUTIVE SUMMARIES VIA GET_POSTS()
$args = array(
'numberposts' => -1,
'orderby' => 'modified',
'order' => 'ASC',
'category_name' => 'executive-summary',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'product_owner',
'value' => 'larry',
'compare' => 'LIKE'
),
array(
'key' => 'product_owner',
'value' => 'sally',
'compare' => 'LIKE'
),
array(
'key' => 'product_owner',
'value' => 'bob',
'compare' => 'LIKE'
)
)
);
$mExecSumPosts = get_posts($args);
//REFERENCE LOOP FOR PREVIEWING REVISIONS
if ($mExecSumPosts) {
foreach ($mExecSumPosts as $post):
setup_postdata($post);
//DEFINE LOOP VARS
$mtitle = $post->post_title;
$mpublisheddate = get_the_time('Y-m-d', $post->ID);
$mformateddate = date("l F jS, Y", strtotime($mpublisheddate));
//OUTPUT DATA
$out.= "<div class='row'>";
$out.= "<div class='col'>";
$out.= "<h3><a href='{$post->post_name}'>{$mtitle}</a> <span class='h5 text-secondary'>: Status Date of {$mpublisheddate} | </span></h3>";
$out.= "</div>";
$out.= "</div>";
endforeach;
}
return $out;
}
wp_reset_postdata();
}
…But! How can I output the product owner name (larry, sally, or bob) in this get_posts loop, for each post that is loaded? I have a feeling I may need to create a second get_posts() loop, but don’t know what that looks like from this point forward. Any help would be awesome!
Solved my issue. I needed to also add
$mproductowners = get_post_meta($post->ID,'product_owner',false)
;
to properly get ACF data from my existing get_posts() loop. From here, I was able to traverse the array and output either larry, sally or bob.
The topic ‘How to also get ACF fields from a get_post loop’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.