Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ACF key in Taxonomy #43

Closed
CesarDenis opened this issue Jun 8, 2016 · 36 comments
Closed

ACF key in Taxonomy #43

CesarDenis opened this issue Jun 8, 2016 · 36 comments
Labels

Comments

@CesarDenis
Copy link

CesarDenis commented Jun 8, 2016

@airesvsg when I request the URL for my custom term:

/wp-json/wp/v2/{taxonomy}

does not come to acf key in json.

and when I request the URL your endpoints:

/wp-json/acf/v2/term/{taxonomy}/{id}

the acf key appears.

Can you help me?
Tks.

@airesvsg
Copy link
Owner

airesvsg commented Jun 8, 2016

Hi @CesarDenis,
your term is public in the rest api?
Thanks

@CesarDenis
Copy link
Author

CesarDenis commented Jun 9, 2016

Yes, the term public.

Term Function

@airesvsg
Copy link
Owner

Hi @CesarDenis,
Sorry for the delay. You're using the ACF inside the theme directory?

@CesarDenis
Copy link
Author

Hi @airesvsg yes, I'm using.

my function.php:

// 1. customize ACF path
function acf_settings_path( $path ) {
    $path = get_stylesheet_directory() . '/lib/acf-pro/';
    return $path;
}
add_filter('acf/settings/path', 'acf_settings_path');

// 2. customize ACF dir
function acf_settings_dir( $dir ) {
    $dir = get_stylesheet_directory_uri() . '/lib/acf-pro/';
    return $dir;
}
add_filter('acf/settings/dir', 'acf_settings_dir');

// 3. Hide ACF field group menu item
if (WP_ENV !== 'development') {
    add_filter('acf/settings/show_admin', '__return_false');
}

// 4. Include ACF
include_once( get_stylesheet_directory() . '/lib/acf-pro/acf.php' );

// 5. Init ACF to rest API
add_action( 'rest_api_init', array( 'ACF_To_REST_API', 'init' ), 5 );

@CesarDenis
Copy link
Author

@airesvsg You can help me?

@airesvsg airesvsg added the bug label Jun 15, 2016
@airesvsg
Copy link
Owner

Yes, I'll fix this bug today.

@CesarDenis
Copy link
Author

CesarDenis commented Jun 15, 2016

Oh, tks 😄

@airesvsg
Copy link
Owner

airesvsg commented Jun 16, 2016

Hi @CesarDenis,
I'll rewrite the plugin, so this moment I suggest using the snippet bellow.

function rest_prepare_customer( $response, $object ) {
    if ( $object instanceof WP_Term ) {
        $response->data['acf'] = get_fields( $object->taxonomy . '_' . $object->term_id );
    }

    return $response;
}

add_filter( 'rest_prepare_customer', 'rest_prepare_customer', 10, 2 );

Thanks

@airesvsg airesvsg mentioned this issue Jun 16, 2016
5 tasks
@CesarDenis
Copy link
Author

Tks @airesvsg. Its work. 😄

@akhanukov
Copy link

Hi @airesvsg. Thanks for the great plugin. I had a similar issue where I can not see the acf data in the basic WP endpoint for a custom taxonomy. I tried the fix above and it does not work. I thought perhaps the rest_prepare_customer was only applicable to @CesarDenis's needs, and tried several other functions to filter into like rest_prepare_ with no luck. Any suggestions? Thanks!

@airesvsg
Copy link
Owner

Hi @akhanukov,
How you are registering your taxonomy?

Thanks

@akhanukov
Copy link

akhanukov commented Jul 13, 2016

Via my functions.php file from within a child theme:

add_action( 'init', 'akd_response_type_taxonomy', 30 );
  function akd_response_type_taxonomy() {
    $labels = array(
        'name'              => _x( 'Response Types', 'taxonomy general name' ),
        'singular_name'     => _x( 'Response Type', 'taxonomy singular name' ),
        'search_items'      => __( 'Search Response Types' ),
        'all_items'         => __( 'All Response Types' ),
        'parent_item'       => __( 'Parent Response Type' ),
        'parent_item_colon' => __( 'Parent Response Type:' ),
        'edit_item'         => __( 'Edit Response Type' ),
        'update_item'       => __( 'Update Response Type' ),
        'add_new_item'      => __( 'Add New Response Type' ),
        'new_item_name'     => __( 'New Response Type Name' ),
        'menu_name'         => __( 'Response Types' ),
    );
    $args = array(
        'hierarchical'      => false,
        'labels'            => $labels,
      'public'             => true,
      'publicly_queryable' => true,
        'show_ui'           => true,
        'show_admin_column' => true,
        'query_var'         => true,
        'rewrite'           => array( 'slug' => 'response_type' ),
        'show_in_rest'       => true,
        'rest_base'          => 'response_types',
        'rest_controller_class' => 'WP_REST_Terms_Controller',
    );
    register_taxonomy( 'response_type', array( 'message' ), $args );
  }

@akhanukov
Copy link

Sorry, still figuring out the Markdown syntax. :)

@airesvsg
Copy link
Owner

@akhanukov I tested here and it's works. What is your url?

@bradmsmith
Copy link

bradmsmith commented Jul 15, 2016

This appears to do it for me using the build-in tags:

function rest_prepare_post_tag( $response, $object ) {
    if ( $object instanceof WP_Term ) {
        $response->data['acf'] = get_fields( $object->taxonomy . '_' . $object->term_id );
    }

    return $response;
}

add_filter( 'rest_prepare_post_tag', 'rest_prepare_post_tag', 10, 2 );

@metinucar
Copy link

@akhanukov

I had the same problem but figured out that the value used for rest_base in taxonomy registration should be used when you create the filter. So, please try the following:

function rest_prepare_[rest_base]( $response, $object ) {...}

add_filter( 'rest_prepare_[rest_base]', 'rest_prepare_[rest_base]', 10, 2 );

@akhanukov
Copy link

Yikes! I just accidentally found this post searching for more clues on my other issue. Sorry for not responding to your last question @airesvsg. Not sure how I missed it. And thank you @bradmsmith and @metinucar for the solutions! I'm going to try these now. I've been using a workaround for the time being successfully, but this is the right way to do it.

@oneguy9
Copy link

oneguy9 commented Feb 26, 2017

Hi there.
I'm trying to figure out how to use this thread.
I have a custom taxonomy called street.
in it I have a custom field -
This is the url for it -
http://80.179.140.91/~shlomit/wp-json/acf/v2/term/street/125

If I do a get - I get this data -

{"acf":{"pws_cityid":99}}

If I'm trying to post it back - like updating it - I write-

{"acf_fields":{"pws_cityid": 99}}

But get this repsonse -

{"code":"cant_update_item","message":"Cannot update item","data":{"status":500}}

I wrote this code in function.php

function rest_prepare_street( $response, $object ) {
    if ( $object instanceof WP_Term ) {
        $response->data['acf'] = get_fields( $object->taxonomy . '_' . $object->term_id );
    }

    return $response;
}

add_filter( 'rest_prepare_street', 'rest_prepare_street', 10, 2 );

and this is the code I used for registering the custom taxonomy ( which is inside projects custom post type -

function street_taxonomy() {

	$labels = array(
		'name'                       => _x( 'רחובות', 'Taxonomy General Name', 'text_domain' ),
		'singular_name'              => _x( 'רחוב', 'Taxonomy Singular Name', 'text_domain' ),
		'menu_name'                  => __( 'רחובות', 'text_domain' ),
		'all_items'                  => __( 'כל הרחובות', 'text_domain' ),
		'parent_item'                => __( 'הורה', 'text_domain' ),
		'parent_item_colon'          => __( 'הורה:', 'text_domain' ),
		'new_item_name'              => __( 'הוסף רחוב', 'text_domain' ),
		'add_new_item'               => __( 'הוסף רחוב חדש', 'text_domain' ),
		'edit_item'                  => __( 'ערוך רחוב', 'text_domain' ),
		'update_item'                => __( 'עדכן רחוב', 'text_domain' ),
		'view_item'                  => __( 'צפה ברחוב', 'text_domain' ),
		'separate_items_with_commas' => __( 'הפרד בין רחובות בפסיק', 'text_domain' ),
		'add_or_remove_items'        => __( 'הוסף או הסר רחוב', 'text_domain' ),
		'choose_from_most_used'      => __( 'בחר מתוך הרחובות הנפוצים', 'text_domain' ),
		'popular_items'              => __( 'רחובות נפוצים', 'text_domain' ),
		'search_items'               => __( 'חפש רחוב', 'text_domain' ),
		'not_found'                  => __( 'לא נמצא', 'text_domain' ),
		'no_terms'                   => __( 'אין רחובות', 'text_domain' ),
		'items_list'                 => __( 'רשימת רחובות', 'text_domain' ),
		'items_list_navigation'      => __( 'ניווט ברחובות', 'text_domain' ),
	);
	$args = array(
		'labels'                     => $labels,
		'hierarchical'               => true,
		'public'                     => true,
		'show_ui'                    => true,
		'show_admin_column'          => true,
		'show_in_nav_menus'          => true,
		'show_tagcloud'              => false,
		'show_in_rest'       => true,
  		'rest_base'          => 'street',
  		'rest_controller_class' => 'WP_REST_Terms_Controller',
	);
	register_taxonomy( 'street', array( 'projects' ), $args );

}
add_action( 'init', 'street_taxonomy', 0 );

Why do I get this response?
Thanks a lot for any help.
Guy

@airesvsg
Copy link
Owner

Hi @oneguy9,
You must have the permission to edit.
See more details here:
https://developer.wordpress.org/rest-api/using-the-rest-api/authentication/
Thanks.

@oneguy9
Copy link

oneguy9 commented Feb 28, 2017

Thanks!
But the thing is that I do have permissions to post.
I do succeed in posting a normal custom field which is not inside a taxonomy.
I still get the same answer over and over -

{
  "code": "cant_update_item",
  "message": "Cannot update item",
  "data": {
    "status": 500
  }
}

I have no idea how to solve this issue -
How do I create this nonce in postman?
Is this the thing i'm missing?

@oneguy9
Copy link

oneguy9 commented Feb 28, 2017

I am able to post acf fields, but the only thing I cannot do is post an acf inside a taxonomy. I'm trying here for 3 days to write all the code i can find on the net with no success and my project is dependent on it.
Any help will be greatly appreciated.
Thanks!
Guy

@airesvsg
Copy link
Owner

airesvsg commented Mar 2, 2017

Hi @CesarDenis,
Yesterday I released a new version ( 3.0.0-beta ).
Please, check it works for you.
Thanks

@oneguy9
Copy link

oneguy9 commented Mar 2, 2017

Hi @airesvsg
I tried installing the v3 but then I didn't get any response from the web site
All I got was a blank page.
I tried changing the path to /v3/ instead of /v2/ but that didn't help at all as well.
I really need your help
This is the only thing that keeps me from publishing the web site.

This is the custom field I'm trying to update -
http://80.179.140.91/~shlomit/wp-json/acf/v2/term/street/125/pws_cityid

Thanks!!!
Guy

@airesvsg
Copy link
Owner

airesvsg commented Mar 2, 2017

Hi @oneguy9,
What was the url you tried with v3?
Thanks

@oneguy9
Copy link

oneguy9 commented Mar 2, 2017

@airesvsg
I tried -
/wp-json/acf/v2/term/{taxonomy}/{id}
and also

/wp-json/acf/v3/term/{taxonomy}/{id}

Both ended up in a blank page....

Thanks for your kind help

@airesvsg
Copy link
Owner

airesvsg commented Mar 2, 2017

@oneguy9 I changed the endpoint in the v3, now you have to access this way:
/wp-json/acf/v3/{taxonomy}/{id}

More details:
https://github.com/airesvsg/acf-to-rest-api#endpoints

Thanks

@oneguy9
Copy link

oneguy9 commented Mar 2, 2017

This URL returns a blank page
http://80.179.140.91/~shlomit/wp-json/acf/v3/street/125/

And I have this in function.php

add_filter( 'acf/rest_api/key', function( $key, $request, $type ) { return 'acf_fields'; }, 10, 3 );
and this

function rest_prepare_street( $response, $object ) { if ( $object instanceof WP_Term ) { $response->data['acf'] = get_fields( $object->taxonomy . '_' . $object->term_id ); } return $response; } add_filter( 'rest_prepare_street', 'rest_prepare_street', 10, 5 );
Thanks!

@airesvsg
Copy link
Owner

airesvsg commented Mar 2, 2017

@oneguy9 please regenerate your permalinks or activate the plugin ACF to REST API.
http://80.179.140.91/~shlomit/wp-json/acf/v2 ( no rest route )
http://80.179.140.91/~shlomit/wp-json/acf/v3 ( no rest route )

@oneguy9
Copy link

oneguy9 commented Mar 2, 2017

Yeah - I think I installed it incorrectly - I will do now some more tests and return to you with results!
Thanks!!!

@oneguy9
Copy link

oneguy9 commented Mar 2, 2017

ok - now the url is working but still I get the same result
http://80.179.140.91/~shlomit/wp-json/acf/v3/street/125/

{"code":"cant_update_item","message":"Cannot update item","data":{"status":500}}

I think I need to write something in function.php for it to work?

Thanks again and sorry for all the trouble...

@airesvsg
Copy link
Owner

airesvsg commented Mar 2, 2017

@oneguy9
Copy link

oneguy9 commented Mar 2, 2017

Yeah I've been there...
How can that help me?
I am able to post to all other acf fields but not to the ones inside taxonomies.

@airesvsg
Copy link
Owner

airesvsg commented Mar 2, 2017

@oneguy9 all fields are editable.
Please, see my examples: https://github.com/airesvsg/acf-to-rest-api#examples
Thanks

@oneguy9
Copy link

oneguy9 commented Mar 2, 2017

I wish I was smarter :(

@airesvsg
Copy link
Owner

airesvsg commented Mar 2, 2017

@oneguy9 please send me an email with your doubts, this way we will not lose the focus of this issue.
airesvsg@gmail.com
Thanks

@AllaManiKumar
Copy link

AllaManiKumar commented Dec 18, 2017

Hi @airesvsg,
my site consists of taxonomies
-videos
-articles
-fun activities
Iam tried this /wp-json/wp/v2/taxonomies at the end of my url ,its working fine but it gives all taxonomies.
My requirment is i want specific taxonomy info. i know this command :
/wp-json/wp/v2/taxonomies/:taxonomyId but problem is i dont know how to find taxonomyId.
I hope you know about this well,so iam waiting for your reply.
-Thanks in advance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants