Support

Account

Home Forums General Issues How can I retrieve fields from an options page using the REST API (v5.11)?

Solved

How can I retrieve fields from an options page using the REST API (v5.11)?

  • I’ve seen this issue touched upon elsewhere in other forum posts, and have reviewed the documentation and the blog post concerning recent updates to the REST API (https://www.advancedcustomfields.com/blog/acf-5-11-release-rest-api/), but I nonetheless can’t figure out how to retrieve field values from a fieldset assigned to an Options page, on the front-end, via the REST API.

    How can I achieve this? I’m newish to using the REST API in general, and I’m sure I’m just missing something obvious.

    Thanks in advance for any help/insight!

  • Seems to be not possible in this version, hopefully it will be added in the future. I also created a post addressing this issue.

    You can use the ACF to REST plugin to solve this issue or do some coding to add the endpoint.

  • Hm — I posted a reply with a a bunch of details, but it seems to have disappeared while editing it for some reason. Bummer 🙁

    Re-posted my original notes below…

  • Thanks to your suggestion I wound up learning how to create a custom endpoint, so thanks for that!

    I wound up creating a custom endpoint that executes and returns the result of a get_field(); call against my options field, which worked a charm.

    Here’s what I wound up with, in case it’s helpful for anyone else trying to do something similar:

    Set up the endpoint and callback:

    
    function cp_colspec_endpoint() {
      $field = get_field('col_spec','options');
      return $field;
    }
    
    //expose acf options page field(s) through custom endpoint
    function cp_register_api_endpoints() {
      register_rest_route( 'canopy/v2', '/acf--colspec', array(
        'methods' => 'GET',
        'callback' => 'cp_colspec_endpoint',
      ) );
    }
    
    add_action( 'rest_api_init', 'cp_register_api_endpoints' );
    
  • Retrieve data from the endpoint in JS (in my case, this is inside a Vue.js method):

    
    fetch('/wp-json/canopy/v2/acf--colspec/')
    .then(resp => {
      // Yay, data!
      return resp.json();
    })
    .then(data => {
      // Do stuff
    })
    .catch(function() {
      // Handle errors
    });
    
  • thanks for this workaround! still, would be great if ACF adds support for options pages in the REST api.

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

You must be logged in to reply to this topic.