Support

Account

Home Forums General Issues Efficient use of get_field

Helping

Efficient use of get_field

  • I’m using advanced custom fields outside of the WordPress loop. I have some custom fields that I’ve added to posts. I have a custom end point for a particular post which returns a number of these custom fields as so:

    get_field('my_field_one', $post_id);
    get_field('my_field_two', $post_id);

    This all works fine, but is this the most efficient way to do it?

    Would it be more efficient to get all the fields at once using get_fields() ? Or perhaps it would be more efficient to set the current post by doing something with setup_postdata($post) first such that I can call get_field('my_field_one') without the second argument.

    What is best practice here? Or is my current approach fine?

  • Hi @willryanuk

    I think what you have is fine. Providing the post_id as the second parameter is sliiiiiighly faster than omitting it since otherwise ACF will use get_the_ID() to fetch the global post id. So it skips that if you provide the post id yourself.

    However if you’re pretty much fetching all the meta you might as well use get_fields since it will only call to get_post_meta (or similar function) once.

Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Efficient use of get_field’ is closed to new replies.