Home › Forums › General Issues › Retrieve fields for post type in home page › Reply To: Retrieve fields for post type in home page
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[0]->ID );
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!
ACF gives Rareloop greater control over the WordPress admin experience. See how in our case study. https://t.co/IFafQupmrl
— Advanced Custom Fields (@wp_acf) May 15, 2023
© 2023 Advanced Custom Fields.
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.