oskarrough / ember-wordpress

The bridge between Ember.js and Wordpress
http://ember-wordpress.surge.sh
MIT License
95 stars 26 forks source link

Using ACF plugin custom endpoint returns only metadata #48

Closed rizkysyazuli closed 5 years ago

rizkysyazuli commented 6 years ago

First of all, sorry if i'm asking this in the wrong repo.

So i've configured the adapter to use custom endpoints provided by ACF to REST API plugin.

// app/adapters/result.js
import WordPress from 'ember-wordpress/adapters/wordpress';

export default WordPress.extend({
  namespace: 'wp-json/acf/v3'
});

But what i get in return is only the ACF metadata. Where's the rest of the WP post data??

tangkapan layar 2018-08-22 20 03 41

NOTE: I want to query posts based on ACF fields. If you guys have other ideas, i'm all open.

oskarrough commented 6 years ago

Hi, please do ask here :) This is helpful information to anyone using ember-wordpress.

To query by ACF fields you'll need to explicitly allow it in your Wordpress theme's functions.php AND use the default endpoint wp-json/wp/v2 and not the ACF one.

Here's an example that will allow you to query a custom post type by a my_field field.

add_filter('rest_{type}_query', function( $args ) {
  $args['meta_query'] = array(
    array(
      'key'   => 'my_field',
      'value' => esc_sql( $_GET['my_field'] ),
    )
  );

  return $args;
});

Remember to replace {type} with the name of your custom post type and my_field with the name of your field..

Then you can filter like this: https://example.com/wp-json/wp/v2/artists?my_field=some-value.

See https://github.com/airesvsg/acf-to-rest-api/issues/13#issuecomment-189403357. I just tested it and got it working. Let me know if it works for you, too. Would be nice to get this into the readme as well.