I need to display a list of Posts from a Custom Post Type (called Recaps), and condition it to only show a Post (Recap) if the currently signed in User ID matches the ID from the advanced custom field I created, which is a User field type.
But there appears to be no way to do this and I cannot locate an documentation on how to achieve this.
Name of field: recap_customers
Field type: Users
My current code:
$args = array(
'orderby' => 'asc',
'post_type' => 'recap',
);
$recaps = get_posts( $args );
foreach ( $recaps as $recap ) : setup_postdata( $recap );
echo "<li>";
$users = get_field('recap_customer', $recap->ID);
foreach ($users as $user) {
echo $user['user_id'];
}
echo '<h3><a href="' . get_permalink($recap->ID) . '">' . $recap->post_title . '</h3>';
echo "</li>";
endforeach;
Can anyone help?
Here’s what I ended up doing, feedback is welcome though:
echo "<ul>";
$args = array(
'orderby' => 'asc',
'post_type' => 'recap',
);
$recaps = get_posts( $args );
$user_ID = get_current_user_id();
foreach ( $recaps as $recap ) : setup_postdata( $recap );
// Get the User selected as Customer.
$field_key = "field_546162f4a416d";
$post_id = $recap->ID;
$field = get_field_object($field_key, $post_id);
$values = $field['value'];
if ( in_array( 898, $values ) ) {
echo '<li><h3><a href="' . get_permalink($recap->ID) . '">' . $recap->post_title . '</a></h3></li>';
} else {
echo "<h4>You do not currently have any Recaps.</h4>";
}
endforeach;