1

This is for a JSON API Endpoint (not a web site). Currently I'm getting all custom fields for a post separately via

get_fields($post_id)

Is there any way to get all associated custom fields with the get_post / get_posts functions?

How I'm merging now:

    try {
        // gather all the posts
        $posts = get_posts(array(
            'posts_per_page'    => 20,
            'orderby'           => 'date',
            'order'             => 'DESC',
            'post_type'         => 'custom_post',
            'post_status'       => 'publish'
        ));
    } catch(Exception $e) {
        return $this->error($e->getMessage());
    }

    try {
        // merge the acf in with the original post data
        for($i = 0; $i < count($posts); ++$i) {
            $post = $posts[$i];

            $acf = get_fields($post->ID);

            $post->acf = $acf;
        }
    } catch(Exception $e) {
        return $this->error($e->getMessage());
    }
  • What's the advantage of this versus calling get_fields() in The Loop while displaying the post? Also, please note that questions regarding 3rd party plugins ( such as Advanced Custom Fields ) is considered to be off-topic here and may be better suited for their support forums. – Howdy_McGee Mar 20 '17 at 15:37
  • Good point. So this function assembles a json object rather than a page (api endpoint). I'll add that to the question, sorry if the OP is misleading. EDIT: I didn't realize the third party piece voids SOF rules. I'll close this question. Thanks for your assistance. – Jacksonkr Mar 20 '17 at 15:39
  • 1
    To bring this back on topic, I would remove references to ACF and trade it with "Custom Fields" as in, "What is the best method to include Custom Fields with the REST API?" or something similar. The concept would still work with ACF just as it would with Custom Fields. – Howdy_McGee Mar 20 '17 at 15:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.