Home › Forums › Front-end Issues › Post object not displaying data
I have several problem when trying to display ACF custom fields data. I’m kind of new to this plugin and have already contacted support but they never get back to me… Can anyone tell me if something’s wrong with my code? Thanks in advance.
Here are two examples :
1. Trying to display post object data (doesn’t display anything on front-end)
<?php
$post_object = get_field('choixsession');
if( $post_object ):
// override $post
$post = $post_object;
setup_postdata( $post );
?>
<div>
<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
<span><?php the_field('objectifsformation'); ?></span>
</div>
<?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<?php endif; ?>
2. Post object data, array shows this and not display name like I need :
Arrayarray(1) { [0]=> array(11) { ["ID"]=> int(24) ["user_firstname"]=> string(4) "Marc" ["user_lastname"]=> string(8) "Gangloff" ["nickname"]=> string(12) "FormateurDOF" ["user_nicename"]=> string(12) "formateurdof" ["display_name"]=> string(12) "FormateurDOF" ["user_email"]=> string(20) "[email protected]" ["user_url"]=> string(0) "" ["user_registered"]=> string(19) "2020-05-12 15:13:09" ["user_description"]=> string(0) "" ["user_avatar"]=> string(259) "" } }
My code:
<?php
$posts = get_field('sessions_a_venir');
if($posts):
?>
<ul>
<?php
foreach( $posts as $post): // ne pas changer $post IMPORTANT
setup_postdata($post);
$event_id = get_the_id();
?>
<li>
<a href="<?php the_permalink(); ?>">
<b>Date :</b> <?php echo tribe_get_start_date(); ?> - <b>Lieu</b> : <?php echo tribe_get_venue(); ?> - <b>Formateur :</b>
<?php echo
$user = get_field('formateur');
if( $user ): ?>
<h3><?php echo $user['display_name']; ?></h3>
<?php endif; ?>
<?php echo rtec_attendance_count_display( $event_id, '<b>Inscrits :</b> {num} sur {max}' ); ?> </a>
</li>
<?php endforeach; ?>
</ul>
<?php
wp_reset_postdata(); // IMPORTANT - réinitialiser l'objet $post sur la requête principale
?>
<?php else: // field_name returned false ?>
<span>Aucune session n'est prévue pour cette formation.</span>
<?php endif; // end of if field_name logic ?>
Thank you. The user field is linked to the post object, as in it’s a data from the post object retrieved from the field “sessions_a_venir”. What should I change to my code?
You need to get the user field and then get the post object field from the user, something like this
$users = get_field('user-field-name');
foreach ($users as $user) {
$posts = get_field('post-object-on-user', $user);
}
Thank you, I know get the correct data but there is an “Array” next to it?
You must be logged in to reply to this topic.
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.