Skip Navigation

[Resolved] Limited field API exposure

This thread is resolved. Here is a description of the problem and solution.

Problem:
The user would like to exclude some fields from REST API calls

Solution:
This will need custom code, check this example:

// 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 );

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/types-api-filters/#toolset_rest_expose_field

This support ticket is created 4 years ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 - - 9:00 – 13:00
14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 - - 14:00 – 18:00

Supporter timezone: Africa/Casablanca (GMT+01:00)

This topic contains 5 replies, has 2 voices.

Last updated by matthewL-7 4 years ago.

Assisted by: Jamal.

Author
Posts
#1841155

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!

#1842123

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.

#1843281

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

#1843493

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. **

#1844143

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

#1844911

Thank you! 🙂