-
-
Notifications
You must be signed in to change notification settings - Fork 113
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 to create a filter to add acf fields from relationship within a repeater, on a custom endpoint. #101
Comments
Hi @akhanukov, add_filter( 'acf/rest_api/messages_in_interaction/get_fields', function( $data, $request, $response ) {
if ( $response instanceof WP_REST_Response ) {
$data = $response->get_data();
}
if ( isset( $data['acf']['attachments'] ) && ! empty( $data['acf']['attachments'] ) ) {
foreach ( $data['acf']['attachments'] as &$a ) {
if ( isset( $a['content_items'] ) && ! empty( $a['content_items'] ) ) {
foreach ( $a['content_items'] as &$c ) {
$c['acf'] = get_fields( $c['ID'] );
}
}
}
}
return $data;
}, 10, 3 ); Thanks |
Thank you for the fast response @airesvsg! I will try it shortly and let you know how it worked! |
Hi @airesvsg, unfortunately, it's not working, unless I'm implementing it wrong. Is there any way I can help debug? I'm going to post my functions that set up this custom endpoint, in case that helps, since I think that perhaps the custom query variable might be messing something up. Though, I did also try without the query variable (
|
Hi @airesvsg, just sending a reminder to the above post. If there's any way to help me help debug this further, I would appreciate it! Thank you for your help! |
Hi @airesvsg, sorry to keep bugging you. I've been trying to make this work without much success. Any hints on other things to try? I can try to rebuild the original custom endpoint if that would help. Thanks! |
Hi @akhanukov, |
I'm unable to send everything like that due to it being somewhat proprietary, but I will gladly send you screenshots of tables and some code. Thank you! |
Ok, try it: // rest_{post_type}_query
add_filter( 'rest_messages_query', function( $args, $request ) {
$interaction = $request->get_param( 'interaction' );
if ( ! is_null( $interaction ) ) {
$args['meta_query'] = array(
array(
'key' => 'content_items_%_content_items',
'compare' => '=',
'value' => (int) $interaction,
)
);
}
return $args;
}, 10, 2 );
add_filter( 'posts_where', function( $where ) {
return str_replace( "meta_key = 'content_items_%", "meta_key LIKE 'content_items_%", $where );
} ); URL: http://mysite.com/wp-json/wp/v2/messages_in_interaction/?interaction=1234 Thanks |
Hey @airesvsg! Thanks for giving it another try. I truly appreciate your plugin and time and efforts outside of it to help me make this work. Unfortunately, I tried adding this code (with interaction as well as interactionID for get_param) and I still can't get |
Hi @airesvsg, I'm still struggling trying to make this work. I still have to send you the files, but is there any simple way to debug filters like this? I found some helpful tools, but they are "within" wordpress's template structure, not in the output of an API. Thanks! |
Hi @akhanukov, |
Hi @airesvsg, after taking a break from this and coming back to see post Post 109, and again trying to implement the filter, I'm convinced that the issue is that messages_in_interaction is not a custom post type, but a filtering of custom post type messages by whether they're associated with an interaction, as you can see in the code above. All of documentation in this project's readme and the filter we're using here, all reference {type} or {post-type}. My hunch is that since messages_in_interaction isn't a post type, the filter doesn't actually do anything. Also, because of the custom endpoint, my normal wp-json routes don't have a messages either, however, there does exist a /acf/v3/messages/. I guess I just want to make sure that's not really the issue this whole time. Thoughts? Thanks for your help! |
Hi @airesvsg, should I open this as a separate ticket? |
Hi @airesvsg, I've looked through all of the issues and see that you send everyone to Issue #9 and the resolution there. I've tried all instances and different combinations and I still can't get it to work, it being a relationship field, within a repeater, on a custom endpoint. Perhaps the issue in my case is that my endpoint isn't just a custom post type, but actually a combination of a custom class controller that replaces/extends WP_REST_Posts_Controller with some logic that queries one custom post type for data in an ACF field and then filters another custom post type based on if that field or data exists. Finally, it is called but a custom query variable. So my url is:
/wp-json/wp/v2/messages_in_interaction/?interactionID=6800 and the output is:
As you'll see, I circled where I would like ACF fields to display. Note, that because I'm using the custom classes, /messages nor /interactions endpoints on their own don't work, yet display as linked data in the API. Any ideas? Thanks!
The text was updated successfully, but these errors were encountered: