-
-
Notifications
You must be signed in to change notification settings - Fork 112
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
How can i get the custom fields of a relationship object? #9
Comments
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 |
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, |
Your solution works in v2, but causes the following fatal error in v3 (the endpoint for this post type is a white screen :().
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:
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 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 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: 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 );
Endpoint: 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....
|
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
The text was updated successfully, but these errors were encountered: