airesvsg / acf-to-rest-api

Exposes Advanced Custom Fields Endpoints in the WordPress REST API
https://wordpress.org/plugins/acf-to-rest-api/
1.32k stars 111 forks source link

Tags returns an empty array? #230

Open octoxan opened 6 years ago

octoxan commented 6 years ago

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.

airesvsg commented 6 years ago

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

octoxan commented 6 years ago

@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 notifications@github.com 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 commented 6 years ago

@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',
    ));

});
airesvsg commented 6 years ago

@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

octoxan commented 6 years ago

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.

airesvsg commented 6 years ago

Hi @octoxan, Can you send me your files and database dump? airesvsg@gmail.com Thanks

octoxan commented 6 years ago

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.

ibanlopez commented 4 years ago

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!!

Koli14 commented 4 years ago

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 commented 4 years ago

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

mehdies7 commented 5 months ago

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