This is my first time working with wordpress, and right now I'm working with a legacy code, migrating some plugins to be gutenberg ready.
There's a plugin that gets all the available tags in the website using this method: get_terms( 'post_tag', array( 'get' => 'all' ) )
.
I would like to know if there is some way to get all the tags in the website but using the WP Data module, like wp.data.select( 'XXX/XXX' ).getTerms('post_tag')
, I haven't found yet a method that does that. Right now I'm getting the tags like this, but I want to know if there is something like getTerms('post_tags')
in wp.data
:
try {
let _the_tags = [];
let api_response = [];
let page = 1;
do {
// Get the tags corresponding to the current page
api_response = await apiFetch( { path: `/wp/v2/tags?per_page=100&page=${ page }`, method: 'GET' } );
// Add the tags to the current tags array
_the_tags = [ ..._the_tags, ...api_response ];
// Increase the page number
page += 1;
} while ( api_response.length > 0 );
// Store the tags in the state
this.setState( { the_tags: _the_tags, loading_tags: false } );
// console.log( this.state.the_tags );
} catch ( error ) {
this.setState( { has_error: true }, () => {
console.log( error );
} );
}
This is my first time working with wordpress, and right now I'm working with a legacy code, migrating some plugins to be gutenberg ready.
There's a plugin that gets all the available tags in the website using this method: get_terms( 'post_tag', array( 'get' => 'all' ) )
.
I would like to know if there is some way to get all the tags in the website but using the WP Data module, like wp.data.select( 'XXX/XXX' ).getTerms('post_tag')
, I haven't found yet a method that does that. Right now I'm getting the tags like this, but I want to know if there is something like getTerms('post_tags')
in wp.data
:
try {
let _the_tags = [];
let api_response = [];
let page = 1;
do {
// Get the tags corresponding to the current page
api_response = await apiFetch( { path: `/wp/v2/tags?per_page=100&page=${ page }`, method: 'GET' } );
// Add the tags to the current tags array
_the_tags = [ ..._the_tags, ...api_response ];
// Increase the page number
page += 1;
} while ( api_response.length > 0 );
// Store the tags in the state
this.setState( { the_tags: _the_tags, loading_tags: false } );
// console.log( this.state.the_tags );
} catch ( error ) {
this.setState( { has_error: true }, () => {
console.log( error );
} );
}
Share
Improve this question
asked Sep 3, 2019 at 18:31
JayJay
214 bronze badges
1 Answer
Reset to default 2Checking in Github and Stackoverflow, I found this way:
wp.data.select( 'core' ).getEntityRecords( 'taxonomy', '<taxonomy_slug>', { per_page: -1, page: 1 } )
In my case the taxonomy slug is post_tag
. So I was able to retrieve all the tags from my website using:
select( 'core' ).getEntityRecords( 'taxonomy', 'post_tag', { per_page: -1, page: 1 } )