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 not showing in Custom Post Type #37

Open
fabioquarantini opened this issue Dec 17, 2015 · 9 comments
Open

ACF not showing in Custom Post Type #37

fabioquarantini opened this issue Dec 17, 2015 · 9 comments

Comments

@fabioquarantini
Copy link

Hello
Thanks for this great plugin.
I'm having trouble retrieving the acf fields in my cpt response.
Currently im making this call:
GET http://website.com/wp-json/wp/v2/locals/id
the "acf" field is not displaying.

This is what I added in my functions.php

add_action( 'init', 'my_custom_post_type_rest_support', 25 );

function my_custom_post_type_rest_support() {
    global $wp_post_types;
    $post_type_name = 'locals';
    if( isset( $wp_post_types[ $post_type_name ] ) ) {
        $wp_post_types[$post_type_name]->show_in_rest = true;
        $wp_post_types[$post_type_name]->rest_base = $post_type_name;
        $wp_post_types[$post_type_name]->rest_controller_class = 'WP_REST_Posts_Controller';
    }
}

Any suggestion?

@matiyin
Copy link

matiyin commented Dec 18, 2015

Sadly I have the same issue. I made a project 2 weeks ago with exactly the same setup/plugins but the 'acf' part is not showing in json, whatever I try to debug. The CPT code in functions.php is identical, a real mystery. I use this part in the create CPT code in functions.php, same as your filter basically:
'show_in_rest' => true,
'rest_base' => 'cities-api',
'rest_controller_class' => 'WP_REST_Posts_Controller',

ACF not showing anywhere, even not pages with some test ACF fields.

Going to try a fresh install and build up again. Keep you updated.

EDIT: fresh install didn't help. I ended up cloning the whole site including database and start from there. Used the exact same theme code of the install that didn't work. JSON outputs the "acf" field now!
aargh. Sorry can't figure out the issue or real difference between the 2 website setups.

@javierbarrera
Copy link

I just ran into a similar issue with one of our sites. We are using CPT-UI and the following versions of these plugins:

  1. Custom Post Type UI 1.1.2
  2. WP REST API 1.2.4
  3. ACF to WP API 1.3.2
  4. Advanced Custom Fields Pro 5.3.2.2

Locally we updated the plugins to the following versions:

  1. Custom Post Type UI 1.1.3
  2. WP REST API 1.2.5
  3. ACF to WP API 1.3.3
  4. Advanced Custom Fields Pro 5.3.2.2

They didn't appear to have any changes that I would consider is breaking, but when we updated locally, the 'acf' object quit showing up in our api calls. We pushed the changes to staging to make sure there wasn't an odd issue locally. The staging site broke as well, so we reverted our changes and re-pushed the plugins to the staging site.

Everything was fixed on the staging site, but for some reason we still have this issue locally.

I can't seem to figure out what would be causing this, since we now have identical code on our local and staging sites. We do use WP Migrate DB Pro to sync environments too, but again, we haven't found the culprit.

I'm going to update plugins on staging until I can break them and see what I can find.

@tr33m4n
Copy link

tr33m4n commented Jan 15, 2016

Any updates on this? Experiencing the same issue :(

@javierbarrera
Copy link

I ended up migrating to WP-API 2 Beta 10. There were some significant changes we had to make on our end for our code to work.

An FYI, we had to visit (not save or update, only visit) the Settings > Permalinks page once the WP-API 2 plugin was installed for the new routes to register. Not sure why we had to do this, but we hit our heads for about 2 hours trying to figure out why the new endpoints weren't present. This happened on both our staging and production sites.

@ryanbeymer
Copy link

For me it was setting 'public' => true, on the registration of the cpt.

@SamThilmany
Copy link

In my case the ACF metabox shows up on the posts post type, but I'm unable to get it working with my CPT's. Whatever I try, the metabox won't show up.

Code of my CTP:

$args = array(
        "label"                   => 'Foo',
        "labels"                  => $labels,
        "public"                  => true,
        "publicly_queryable"      => true,
        "show_ui"                 => true,
        "show_in_rest"            => true,
        "rest_base"               => 'foo',
        "rest_controller_class"   => 'WP_REST_Posts_Controller',
        "has_archive"             => false,
        "show_in_menu"            => true,
        "exclude_from_search"     => false,
        "capability_type"         => array('foo', 'foos'),
        "capabilities"            => array(
                                          'edit_post'          => 'edit_foo',
                                          'read_post'          => 'read_foo',
                                          'delete_post'        => 'delete_foo',
                                          'edit_posts'         => 'edit_foos',
                                          'edit_others_posts'  => 'edit_others_foos',
                                          'publish_posts'      => 'publish_foos',
                                          'read_private_posts' => 'read_private_foos',
                                          'create_posts'       => 'create_foo'
                                     ),
        "map_meta_cap"            => true,
        "hierarchical"            => true,
        "rewrite"                 => array(
                                          "slug" => "foo",
                                          "with_front" => true
                                     ),
        "query_var"               => true,
        "menu_position"           => 5,
        "supports"                => array(
                                          "title",
                                          "editor",
                                          "thumbnail",
                                          "excerpt",
                                          "comments",
                                          "revisions",
                                          "author"
                                     ),
        "taxonomies"              => array( "foo_tax" ),
);

@sebastienfi
Copy link

👍

@erasmoh
Copy link

erasmoh commented Feb 6, 2021

Still no fix about this? I have the same issue.

@erasmoh
Copy link

erasmoh commented Feb 6, 2021

I just found this solution. You just need to add this code to your functions.php and it would show all your post object ACF API.

$post_types = array_merge(get_post_types(), cptui_get_post_type_slugs());

foreach ($post_types as $type) {
	add_filter(
		'acf/rest_api/' . $type . '/get_fields',
		function ($data, $response) use ($post_types) {
			if ($response instanceof WP_REST_Response) {
				$data = $response->get_data();
			}

			array_walk_recursive($data, 'get_fields_recursive', $post_types);

			return $data;
		},
		10,
		3
	);
}

function get_fields_recursive($item)
{
	if (is_object($item)) {
		$item->acf = array();

		if ($fields = get_fields($item)) {
			$item->acf = $fields;
			array_walk_recursive($item->acf, 'get_fields_recursive');
		}
	}
}

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

No branches or pull requests

8 participants