Skip to content

Option for TaxonomyControl to use apiFetch #14

Description

@lauravicuna-tech

If a taxonomy has capabilities set, useSelect won't return the terms due to a rest_forbidden_context error. Could we have an option to pass a prop to toggle between using apiFetch and useSelect in cases where we might need to maintain capability restrictions but still be able to fetch terms?

import apiFetch from '@wordpress/api-fetch';

...
const [ apiData, setApiData ] = useState( null );

/**
 * Get all taxonomies of slug type
*/
useEffect( () => {
	if ( ! useApiFetch ) {
		return;
	}

	apiFetch( { path: `/wp/v2/${ slug }` } ).then( ( posts ) => {
		setApiData( posts );
	} );
}, [ slug, useApiFetch ] );

const selectData = useSelect(
	( select ) => {
		return select( 'core' ).getEntityRecords( 'taxonomy', slug, {
			per_page: -1,
		} );
	},
	[ slug ]
);

const data = useApiFetch ? apiData : selectData;
<TaxonomyControl
	...
	useApiFetch={ true }
/>

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions