I am making a function where I pass custom post id and get the acf field back.
Any suggestion how to correctly do that ?
Right now I have something like this, but it wont work:
function getMenuCatergory($post_id)
{
$objects = new WP_Query([
'post_type' => 'rokasgramata',
'p' => $post_id,
'meta_key' => 'category',
]);
return $objects;
}
You are getting the post. To get a field value you just get that value
return get_field('category', $post_id);
but I’m confused why you would be building a functions for something that ACF already does.
Thank you! Will remember this solution too. But I get everything done by using get_post_meta
get_post_meta() will not work for all types of fields, complex fields will not be formatted correctly. But you can use use that function with the post ID, there is no need to get the post beforehand https://developer.wordpress.org/reference/functions/get_post_meta/