简体   繁体   English

WordPress REST API全局搜索(API V2)

[英]WordPress REST API Global Search (API V2)

I am looking for a way to run a global search query across all or multiple post types using WP REST API. 我正在寻找一种使用WP REST API跨所有或多个帖子类型运行全局搜索查询的方法。

I am able to search posts using: 我可以使用以下方式搜索帖子:

http://example.com/wp-json/wp/v2/posts?search=test

In turn I am able to search pages using: 反过来我可以使用以下方法搜索页面:

http://example.com/wp-json/wp/v2/pages?search=test

How do I search across both posts and pages? 如何搜索帖子和页面? I was able to do this in WP REST API V1 by specifying multiple type[] variables in the query? 通过在查询中指定多个type[]变量,我能够在WP REST API V1中执行此操作吗?

This might be a bit late but there is an endpoint for that in the v2-api: /wp-json/wp/v2/search . 这可能有点晚,但在v2-api中有一个端点: /wp-json/wp/v2/search

You can search for any specific post_type by supplying it via subtype or leave it to the default ( any ) to search in all post_types. 您可以通过subtype提供任何特定的post_type ,或者将其保留为默认值( any )以在所有post_types中搜索。

Edit: Of course you can also specifiy multiple with an array as you did before. 编辑:当然你也可以像以前一样用数组指定多个。

You cannot search multiple post types via the get query parameters. 您无法通过get查询参数搜索多个帖子类型。 It was an intentional design decision to restrict the API to one post type by default. 这是一个故意的设计决定,默认情况下将API限制为一种帖子类型。 You simply can't do it by with v2. 你不能用v2来做到这一点。 However there is a plugin in the .org repo that adds this feature. 但是.org repo中有一个插件可以添加此功能。

https://wordpress.org/plugins/rest-api-multiple-post-types/ https://wordpress.org/plugins/rest-api-multiple-post-types/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM