在Gutenberg组件中跨帖子类型搜索关键字

时间:2021-06-01 作者:leemon

在我正在开发的Gutenberg组件中,我有一个函数,可以在常量中指定的多个帖子类型中搜索关键字:

const postTypes = [ \'posts\', \'pages\', \'portfolio\' ];

const onSearchStringChange = ( keyword ) => {
    Promise.all( postTypes.map( postType => apiFetch( {
        path: `/wp/v2/${ postType }?search=${ keyword }`,
    } ) ) ).then( ( results ) => {
        console.log( results.reduce( ( result, final ) => [...final, ...result], [] ) );
    } )
};
该函数按预期工作并返回一个结果数组。

我希望获得可查看的帖子类型(减去attachment post type)通过函数访问站点。这就是我正在使用的:

const postTypes = useSelect( ( select ) => {
    const { getPostTypes } = select( \'core\' );
    const excludedPostTypes = [ \'attachment\' ];
    const filteredPostTypes = getPostTypes( { per_page: -1 } )?.filter(
        ( { viewable, slug } ) => viewable && ! excludedPostTypes.includes( slug )
    );
    const result = ( filteredPostTypes || [] ).map( ( { slug } ) => slug );
    return result;
}, [] );
该函数也按预期工作,我得到了一个所有可查看帖子类型的slug数组attachment.

但是,如果我将结果数组传递给onSearchStringChange 函数我收到以下错误:

Uncaught (in promise) 
Object { code: "rest_no_route", message: "No route was found matching the URL and request method.", data: {…} }
​code: "rest_no_route"
​data: Object { status: 404 }
​message: "No route was found matching the URL and request method."
​<prototype>: Object { … }
我假设在调用onSearchStringChange 作用即使我做了postTypes.length > 0apiFetch 我也有同样的错误。

所以,我的问题是:在调用时,如何确保post-types数组被完全解析onSearchStringChange?

2 个回复
最合适的回答,由SO网友:leemon 整理而成

而不是进行多次apiFetch 对于每个帖子类型,跨多个帖子类型进行搜索的最佳解决方案是使用WP REST APIsearch 端点,直到现在我才知道它存在。缺点是,使用此端点无法获取完整的post对象,但如果只需要post id、标题、url和类型,则可以:

const postTypes = useSelect( ( select ) => {
    const { getPostTypes } = select( \'core\' );
    const excludedPostTypes = [ \'attachment\' ];
    const filteredPostTypes = getPostTypes( { per_page: -1 } )?.filter(
        ( { viewable, slug } ) => viewable && ! excludedPostTypes.includes( slug )
    );
    const result = ( filteredPostTypes || [] ).map( ( { slug } ) => slug );
    return result;
}, [] );

const onSearchStringChange = ( keyword ) => {
    if ( postTypes.length > 0 ) {
        apiFetch( {
            path: `/wp/v2/search?search=${ keyword }&type=post&subtype=${ postTypes.join() }`,
        } ).then( ( results ) => {
            console.log( results );
        } )
    }
};

SO网友:leemon

我回答自己是为了他人的利益。我正在将一组post-type Slug传递给apiFetch 而不是通过rest_base 参数因此,正确的功能应该是:

const postTypes = useSelect( ( select ) => {
    const { getPostTypes } = select( \'core\' );
    const excludedPostTypes = [ \'attachment\' ];
    const filteredPostTypes = getPostTypes( { per_page: -1 } )?.filter(
        ( { viewable, slug } ) => viewable && ! excludedPostTypes.includes( slug )
    );
    const result = ( filteredPostTypes || [] ).map( ( { rest_base } ) => rest_base );
    return result;
}, [] );

相关推荐

ARRAY_MERGE内的Foreach循环

我需要在下拉菜单中获得用户显示名称的列表。我有查询权限,但我不确定如何准确地将我的用户foreach放入数组中。以下是我需要将用户注入的代码:$data[\'settings\'][\'advanced_options\'] = array_merge($data[\'settings\'][\'advanced_options\'], [ [ "label" => "Dynamic Option 1", //