最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

block editor - How to get all the available post tags using the WP Data Module?

programmeradmin0浏览0评论

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
Add a comment  | 

1 Answer 1

Reset to default 2

Checking 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 } )

发布评论

评论列表(0)

  1. 暂无评论