0

This snippet of PHP code is inside an Elementor template where I display posts from custom post type "proprieta" (English = property). I need to retrive details of the sales representative for that property. There are 2 ACF field group: one for "proprieta" and one for "venditore" (salesman). Inside the ACF field group for "proprieta" I added an object-field "venditori_proprieta" as to create a relation between the two field groups.

Now this is the code to display fields from the "venditore" field group:

  $posts = get_posts(array(
    'post_type'     => 'venditore'
)
   );

   if( $posts ):
        foreach( $posts as $post ): $venditore_id = get_field('venditore_proprieta');

        $venditore_nome = get_field('nome_venditore', $venditore_id);
        echo '<div class="foto">' . $venditore_nome . '</div>';
        
        $venditore_photo = get_field('venditore_immagine', $venditore_id);
        echo '<div class="foto">' . $venditore_photo . '</div>';

    endforeach; 
endif; 
wp_reset_postdata(); ?>

still, nothing is displaying. Any hint? I'd appreciate your help.

  • Have you verified what `$venditore_id` actually contains? – CBroe Aug 17 '23 at 09:58
  • Mmm, no I didn't. Actually I w'dnt know how to do it. – Davide Bellucci Aug 17 '23 at 10:27
  • `var_dump($venditore_id);` – CBroe Aug 17 '23 at 10:33
  • Pretty sure it _won't_ contain what you expected, because you did not actually "set up" your post in your foreach loop there - so `get_field` will use the ID of whatever page you are currently on, not the individual post you are trying to output there. Pass the post id explicitly as second parameter. – CBroe Aug 17 '23 at 10:35
  • Yes, you're right. It returns NULL. So, how can I get the venditore ID? – Davide Bellucci Aug 18 '23 at 05:56
  • Like I said - pass the post id to get_field explicitly. – CBroe Aug 18 '23 at 06:27
  • I tryed this way: foreach( $posts as $post ): $venditore_id = get_field('venditore_proprieta', $post->ID); but I am going nowhere. Help! – Davide Bellucci Aug 20 '23 at 08:30
  • If this is a sub-field in a repeater, then you need to get the repeater field first, and then loop over the sub-fields. https://www.advancedcustomfields.com/resources/get_sub_field/ – CBroe Aug 22 '23 at 05:55

0 Answers0