acf-to-rest-api icon indicating copy to clipboard operation
acf-to-rest-api copied to clipboard

Tags returns an empty array?

Open octoxan opened this issue 7 years ago • 11 comments

I've added a color picker for tags, and am trying to pull that over the API and all I'm getting is an empty array?

Here's what I get when pulling it via the regular built in REST API.

    "id": 11,
    "count": 1,
    "description": "",
    "link": "http://example.test/tag/latest-tag/",
    "name": "Latest Tag",
    "slug": "latest-tag",
    "taxonomy": "post_tag",

Here is what I'm getting when accessing example.test/wp-json/acf/v3/tags?hide_empty=0 or example.test/wp-json/acf/v3/tags

[
  
]

I do have "available via REST API" set to Yes.

Edit: When I go to example.test/wp-json/acf/v3/posts/40 I can see actual results, so I know the API is indeed working at certain points. I have the same field on posts and tags, and can see the color picker results on posts, but not tags.

octoxan avatar Apr 10 '18 20:04 octoxan

Hi @octoxan,

When you register a new tag, you need to use the parameter show_in_rest to expose the data in REST API.

Follow the link below for more detail: https://github.com/airesvsg/acf-to-rest-api/issues/212#issuecomment-367817839

https://developer.wordpress.org/reference/functions/register_taxonomy/#parameters

Thanks

airesvsg avatar Apr 10 '18 21:04 airesvsg

@airesvsg

How would I do this for just default WordPress post_tags? I've applied a custom field to the default post tag taxonomy. Is this ACF api able to access fields applied to post_tags? Would I just re-register the taxonomy?

On Tue, Apr 10, 2018, 5:34 PM Aires Gonçalves [email protected] wrote:

Hi @octoxan https://github.com/octoxan,

When you register a new tag, you need to use the parameter show_in_rest to expose the data in REST API.

Follow the link below for more detail:

https://developer.wordpress.org/reference/functions/register_taxonomy/#parameters

Thanks

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/airesvsg/acf-to-rest-api/issues/230#issuecomment-380254341, or mute the thread https://github.com/notifications/unsubscribe-auth/AELd8wDoebH0QJXK1F9d8Jsha1VYCDaGks5tnSVRgaJpZM4TO_Dq .

octoxan avatar Apr 10 '18 22:04 octoxan

@airesvsg

Just tried the following with no luck:

add_action('init', function() {
    
    register_taxonomy('post_tags', array(
        'public'       => true,
        'label'        => __( 'Tags' ),
        
        // rest api parameters
        'show_in_rest' => true,
        'rest_base'    => 'tags',
    ));
    
});

octoxan avatar Apr 11 '18 13:04 octoxan

@octoxan post_tags is enabled to expose in REST API by default.

How you are saving your custom fields? Via REST API or Admin?

Thanks

airesvsg avatar Apr 11 '18 13:04 airesvsg

Admin. I added color to both post_tags and to posts (to make sure it was visible somewhere, and it does indeed show up when using the API to query posts). The tag shows up in the regular wordpress API, but I can't seem to get it and it's custom fields through the ACF REST API.

octoxan avatar Apr 11 '18 13:04 octoxan

Hi @octoxan, Can you send me your files and database dump? [email protected] Thanks

airesvsg avatar Apr 11 '18 16:04 airesvsg

Sadly I cannot as it's sensitive client files.

I can tell you how to recreate the issue though if you want.

  1. Install new WP install using Roots Bedrock https://roots.io/bedrock/docs/installing-bedrock/
  2. Install new WP theme, Roots Sage https://roots.io/sage/docs/theme-installation/
  3. Install ACF and your plugin using Composer and wp-packagist
  4. Add a new custom field, color picker, for post_tags.
  5. Add a color to the tag, and the tag to some posts.
  6. View API for post_tags

Sorry, I hope that helps a little somehow.

I'm not just accessing the post tags and their color through normal acf and converting it to json in a blade (php) template so I have access to all the tags colors on every page in javascript.

Feel free to close the issue if you want to, or if you can't reproduce it. I'm guessing it might have something to do with Bedrock/Sage being non-typical Wordpress installs.

octoxan avatar Apr 11 '18 17:04 octoxan

Hi,

Any solution to this? I have same problem trying to fetch ACF fields on tags, but I get an empty array

=== [ Fetching wordpress__acf_tags ] === https://example.com/wp-json/acf/v3/tags -> wordpress__acf_tags fetched : 0 Fetching the wordpress__acf_tags took: 328.455ms

That result is due to that empty array on that endpoint.

When in categories works just fine

=== [ Fetching wordpress__acf_categories ] === https://example.com/wp-json/acf/v3/categories -> wordpress__acf_categories fetched : 7 Fetching the wordpress__acf_categories took: 312.396ms

post_tags should show in rest api by default, any idea why this is not working?

Thanks!!

ibanlopez avatar Jul 24 '19 12:07 ibanlopez

I have the same problem. If I visit /wp-json/wp/v2/tags/471 there is no ACF field. By a Category or a Custom Taxonomy, it's working (e.g /wp-json/wp/v2/categories/4 or wp-json/wp/v2/folders/4. If I go to /wp-json/acf/v3/tags it's an empty array, but working for Categories or Custom Taxonomy. If I visit /wp-json/acf/v3/tags/471, then it has an ACF field as exepted. But sadly I can not use that, as I'm creating an app with frontity, and I have no idea how could I reach that point...

Koli14 avatar Feb 21 '20 15:02 Koli14

This solves the issue: https://github.com/airesvsg/acf-to-rest-api/issues/245#issuecomment-415484050

Koli14 avatar Feb 27 '20 14:02 Koli14

This happens to me every few months and the working solution for me is to turn off "Show in REST API" and hit Save and then turn in on again

mehdies7 avatar Jan 11 '24 07:01 mehdies7