How can i get the custom fields of a relationship object?
Pxlat opened this issue ยท 17 comments
I have a custom post type (Orders) which has custom fields and another custom post type called (address) which also has custom fields, when you add a new order it will ask you to choose an address (post object), When i call the posts endpoint of the type (Orders), it gets only the core fields of (address) post object but i cannot see the (address) custom fields
Hi @Pxlat,
you should use the filter "acf/rest/{type}/get_fields" to append this data.
add_filter( 'acf/rest_api/orders/get_fields', function( $data ) {
if ( $data ) {
$data->address['acf'] = get_fields( $data->id );
}
return $data;
}, 10, 1 );
I used the variable $data as object in my example, but can be array.
Can you post the structure of your response?
That didn't work for me
Orders endpoint http://s15695.p20.sites.pressdns.com/wp-json/wp/v2/wimo-order/
Address endpoint http://s15695.p20.sites.pressdns.com/wp-json/wp/v2/adress/
Try it:
add_filter( 'acf/rest_api/wimo-order/get_fields', function( $data, $request, $response ) {
if ( $response instanceof WP_REST_Response ) {
$data = $response->get_data();
}
if( isset( $data['acf'] ) ) {
$data['acf']['shipping_address']->acf = get_fields( $data['acf']['shipping_address']->ID );
}
return $data;
}, 10, 3 );
It worked a like charm
This helped fix an issue of mine. Should post that code snippet in the documentation.
Hi @jsanc623,
yes, I'll do this.
Thanks
Your solution works in v2, but causes the following fatal error in v3 (the endpoint for this post type is a white screen :().
Uncaught ArgumentCountError: Too few arguments to function Jacobin_Rest_API_Fields::filter_issue_acf_response(), 2 passed in /srv/www/editor-trellis.jacobinmag.com/current/web/wp/wp-includes/class-wp-hook.php and exactly 3 expected :281
My code:
/**
* Filter ACF_To_REST_API Response
*
* @since 0.3.8
*/
add_filter( 'acf/rest_api/issue/get_fields', array( $this, 'filter_issue_acf_response' ), 10, 3 );
/**
* Add Meta to Issue Response
* Modify response from ACF_To_REST_API
*
* @link https://github.com/airesvsg/acf-to-rest-api/issues/9
*
* @since 0.3.8
*
* @param obj $data
* @param obj $request
* @param obj $response
* @return obj $data
*/
public function filter_issue_acf_response( $data, $request, $response ) {
if ( $response instanceof WP_REST_Response ) {
$data = $response->get_data();
}
if( isset( $data['acf']['article_issue_relationship'][0] ) ) {
$issue_id = $data['acf']['article_issue_relationship'][0]->ID;
if( $issue_id ) {
$data['acf']['article_issue_relationship'][0]->issue_number = (int) get_post_meta( $issue_id, 'issue_number', true );
$data['acf']['article_issue_relationship'][0]->issue_season = get_post_meta( $issue_id, 'issue_season', true );
}
}
return $data;
}
Any idea why this is and who it can be fixed?
Hi @misfist,
Read again:
Uncaught ArgumentCountError: Too few arguments to function Jacobin_Rest_API_Fields::filter_issue_acf_response(), 2 passed in /srv/www/editor-trellis.jacobinmag.com/current/web/wp/wp-includes/class-wp-hook.php and exactly 3 expected :281
Now, replace 3 to 2 in below code ๐
add_filter( 'acf/rest_api/issue/get_fields', array( $this, 'filter_issue_acf_response' ), 10, 3 );
In V3 the $response
parameter not exists.
public function filter_issue_acf_response( $data, $request ) {
// your code here
}
https://github.com/airesvsg/acf-to-rest-api#filters
Thanks
Thanks @airesvsg.
Changing the number of expected arguments alone doesn't solve the problem.
The issue is that the 3rd argument $response
, which is needed by the function is now provided as the 2nd argument. So, I needed to change the function to:
public function filter_issue_acf_response( $data, $response ) {
if ( $response instanceof WP_REST_Response ) {
$data = $response->get_data();
}
if( isset( $data['acf']['article_issue_relationship'][0] ) ) {
$issue_id = $data['acf']['article_issue_relationship'][0]->ID;
if( $issue_id ) {
$data['acf']['article_issue_relationship'][0]->issue_number = (int) get_post_meta( $issue_id, 'issue_number', true );
$data['acf']['article_issue_relationship'][0]->issue_season = get_post_meta( $issue_id, 'issue_season', true );
}
}
return $data;
}
Also, these changes breaks version 2. So, I added a condition to get the version.
V2
add_filter( 'acf/rest_api/{type}/get_fields', function( $data, $response, $request ) {
}, 10, 3 );
V3
add_filter( 'acf/rest_api/{type}/get_fields', function( $data, $response ) {
}, 10, 2 );
@airesvsg - Yes, I have it working now.
I added this:
$this->version = get_option( 'acf_to_rest_api_request_version' );
/**
* Filter ACF_To_REST_API Response
*
* @since 0.3.8
* @since 0.4.7
*/
if( '2' == $this->version ) {
add_filter( 'acf/rest_api/issue/get_fields', array( $this, 'filter_issue_acf_response_v2' ), 10, 3 );
} else {
add_filter( 'acf/rest_api/issue/get_fields', array( $this, 'filter_issue_acf_response' ), 10, 2 );
}
Is there a way to specify a page slug, in this filter?
Example:
add_filter( 'acf/rest_api/page/get_fields?slug=home', function( $data, $response ) {
// Do stuff...
}, 10, 2 );
Hi @zacksmash,
You can use those ways to specify whats do you want filter.
Page url: http://localhost/my-slug
add_filter( 'acf/rest_api/page/get_fields', function( $data, $response ) {
$id = $response->get_param( 'id' );
if ( 'my-slug' === get_post_field( 'post_name', $id ) ) {
// Do stuff
}
return $data;
}, 10, 2 );
OR
Endpoint: http://localhost/wp-json/acf/v3/pages/2
add_filter( 'acf/rest_api/page/get_fields', function( $data, $response ) {
$id = $response->get_param( 'id' );
if ( 2 === absint( $id ) ) {
// Do stuff
}
return $data;
}, 10, 2 );
Thanks
@airesvsg Thanks for the reply!
That's what I am currently doing, I just wanted to make sure that it was the recommended way. It's be nice to have a way to specify a specific page by ID or slug, but this works for now!
I'm not sure if this is the right place to ask for help but I'm struggling with getting the title of a post object field in my relationship filter. You'll see community referenced in my code below. The custom field 'show_as_community_model' is the relationship field that allows you to select a 'home_listing' and 'community' is a post object that resides in the home listing information field group of ACF. So why won't the 'community' post object field title show up in the filter. All the other simple ACF text fields show up perfectly using my code here....
add_filter('acf/fields/relationship/result/name=show_as_community_model', 'my_acf_fields_relationship_result', 10, 4);
function my_acf_fields_relationship_result( $text, $post, $field, $post_id ) {
$community = get_field( 'community', $post->ID );
$sqft = get_field('sq_ft', $post->ID);
$mls = get_field('mls', $post->ID);
$price = get_field('price',$post->ID);
if ($post->post_type == 'home_listings') {
$text .= ' โข Community:' . $community . ' โข Lot: ' . $lot . ' โข SqFt: ' . $sqft . ' โข MLS: ' . $mls . ' โข Price: ' . $price . '';
}
return $text;
}