Hi there,
I have enabled the Toolset API on a CPT which is working great but I really need to hide some fields from the results.
I tried to look at your snippet example but I couldn't follow it, I imagine its simple but my knowledge of PHP is very limited.
Is there any chance you could let me know how I can use the snippet to exclude fields:
wpcf-affiliate-network and wpcf-notes from the API for CPT "offer"?
Thank you!
Hello and thank you for contacting the Toolset support.
I could not find an example on our documentation page about the Toolset hooks for filtering fields in REST API Calls and I have informed our documentation team about it. We'll get an example very soon.
So, I run a small test locally and I was able to hide a custom field(slug: book-additional-notes) from a specific custom post type Book(slug: book) with the following code.
// This is to enable the filtering at all.
add_filter( 'toolset_rest_run_exposure_filters', '__return_true' );
// exclude "book-additional-notes" custom field from "book" custom post type.
add_filter( 'toolset_rest_expose_field', function(
$expose_field, $domain, $group_slug, $field_slug, $element_type, $element_id
) {
if( 'posts' === $domain && 'book' == $element_type && 'book-additional-notes' == $field_slug ) {
return false;
}
// Different case - do not alter the result.
return $expose_field;
}, 10, 6 );
For your case, the following code should work:
add_filter( 'toolset_rest_expose_field', function(
$expose_field, $domain, $group_slug, $field_slug, $element_type, $element_id
) {
if( 'posts' === $domain && 'offer' == $element_type && 'affiliate-network' == $field_slug ) {
return false;
}
if( 'posts' === $domain && 'offer' == $element_type && 'notes' == $field_slug ) {
return false;
}
// Different case - do not alter the result.
return $expose_field;
}, 10, 6 );
Note that I did not use the "wpcf-" prefix for checking field_slug.
I hope this helps. Let me know if you have any questions.
Hmm that didn't work :-/
Not sure why. I copied exactly as the field names etc are correct, well apart from I realised notes was called note so I changed this. So the field names are 100% correct.
Any ideas why it is still exposing the fields?
API call I am loading is: hidden link
Cheers
I can't really tell without checking the actual results of the REST calls.
Would you allow me temporary access to your website to check this further? Your next reply will be private to let you share credentials safely. ** Make a database backup before sharing credentials. **
You forgot to add the first line of the custom code, which will activate the filtering of custom fields.
// This is to enable the filtering at all.
add_filter( 'toolset_rest_run_exposure_filters', '__return_true' );
I added it and it is now working. Check this screenshot hidden link