I have CPT Call product with 4 custom fields
I want to reterive field [pref] in home page with the latest product listed in the post type.
Can anyone help me ?
May this is, what you are looking for. First, the get_posts() function gets the posts from the post type “product” ordered by date with the latest date first and returning only the first result by ‘numberposts’ => 1. Then get the field value of that post using its ID.
$latest_post = get_posts(array(
'numberposts' => 1, // restrict result to only the latest post
'post_status' => 'publish',
'post_type' => 'product',
'orderby' => 'date',
'order' => 'DESC'
$prev = get_field( 'pref', $latest_post->ID );
It’s work as <?php the_field(‘prefs’); ?> !