mathias5
mathias5

Reputation: 311

Cannot get ACF from custom post type post

I have a custom post type called "members", which has an ACF field connected. I try to get the content of that field. But when I try, I only get the regular post object. Not the ACF fields connected.

Here is what i am trying, but only getting the post object.

`

$featuredmembers = get_field('featured_member');
global $post;

//$featuredmembers has a field named "featured". That's the field I want.


$posts = get_posts([
  'post_type' => 'members',
  'post_status' => 'publish',
  'posts_per_page' => -1,
  'order' => 'ASC',
  'orderby' => 'title'
]);


foreach ($featuredmembers as $post) {
  print_r($post['featured']->ID);
  echo get_field('featured');
}

`

Upvotes: 0

Views: 2019

Answers (2)

Luke Tubby
Luke Tubby

Reputation: 69

I think this might be down the foreach loops using a variable of $post. This will override the main $post variable of the page, and any get_field function calls after the foreach loop will be looking at the wrong post.

Try renaming those variables to something other than $post

Upvotes: 1

giolliano sulit
giolliano sulit

Reputation: 1006

Try passing the post id in the get_field / the_field call within the foreach loop:

foreach ($featuredmembers as $post) {
    // the_field('featured', $post->ID);
    echo get_field('featured', $post->ID);
}

If it's a repeater field you can use:

foreach ($featuredmembers as $post) {
    the_repeater_field('featured', $post->ID);
}

Upvotes: 1

Related Questions