Home › Forums › General Issues › Related Posts based on ACF Fields › Reply To: Related Posts based on ACF Fields
Hi @neodjandre
Do you mean you want to get the custom field value from inside a custom shortcode? I believe you can do that by passing the current post ID to the get_field()
function like this:
// Get the current post object first
global $post;
$location = get_field('location', $post->ID);
To get the related posts, you can always query them based on custom field value like this:
// get the value without the formatting so we can search it in the database
$location = get_field('location', $post->ID, false);
$posts = get_posts(array(
'numberposts' => 5,
'post_type' => 'custom-post',
'meta_key' => 'location',
'meta_value' => $location
));
This page should give you more idea about it: https://www.advancedcustomfields.com/resources/query-posts-custom-fields/.
I hope this helps 🙂
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.