• Hi there

    I activated a endpoint for a custom post type, and it returns the fields/meta I’ve set in parameters.

    But when I want to filter the result with the _fields argument, I get only fields I want, ok … but the meta doesn’t have value anymore (its null !)

    I have miss something ?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Contributor jpowersdev

    (@jpowersdev)

    Hi @kaon68,

    I am able to filter the API results using the fields parameter on a local test install. I also confirmed that querying for a nonexistent field simply omits the field, it isn’t null (so it can’t be that). Do you have any other networking-related plugins that may be interfering?

    Thanks,
    Jon

    Thread Starter kaon68

    (@kaon68)

    Just to complete my problem with an example :
    https://drive.google.com/file/d/1j4OvqOPruyA9YUWxNHsUdeJmzBj97e-H/view?usp=sharing
    https://drive.google.com/file/d/1OGeJD944Vcp2vgazQ6ZeCr2J7rvyQW3f/view?usp=sharing

    first link is for unfiltered result
    second link I have added “&_fields=id,title.rendered,_bbp_forum_id,_bbp_topic_id,_bbp_reply_to,uagb_author_info.display_name” on my request

    For network related extensions, API Bearer Auth to limit to authenticated request.

    Plugin Contributor jpowersdev

    (@jpowersdev)

    Hi @kaon68,

    This is a similar problem to this one here. Basically, when you add query parameters to the request, it is no longer handled by our plugin’s customization. Since it is then a regular API request, you need to register the meta fields with WordPress.

    Something like this is necessary for each meta field:

    add_action('rest_api_init', function(){
    	register_rest_field('post-type-slug', 'meta_key', array(
    		'get_callback' => function($params){
    			return \get_post_meta($params['id'], 'meta_key', true);		
    		}
    	));
    });

    This can be done in a loop if you first define an array of necessary meta keys, then loop through the keys inside the rest_api_init action and register the get_callback for each one (as seen above).

    Let me know if that helps,
    Jon

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘using _fields parameter nullify meta keys ?’ is closed to new replies.