简体   繁体   English

WP REST API查询多种帖子类型

[英]WP REST API query multiple post types

I am trying to retrieve posts from multiple post types using WP REST API. 我正在尝试使用WP REST API从多种帖子类型检索帖子。 I have no problem getting a feed from one post type book by doing this: 通过执行以下操作,我可以从一本邮政类型的book获取供稿:

http://example.com/wp-json/posts?type=book&filter[posts_per_page]=10

Now I want to extend the feed to get book and movie . 现在,我想扩展提要以获取bookmovie This only gives me the last specified type: 这只给了我最后指定的类型:

http://example.com/wp-json/posts?type=book&type=movie&filter[posts_per_page]=10

This gives me an error: 这给我一个错误:

http://example.com/wp-json/posts?type[]=book&type[]=movie&filter[posts_per_page]=10

How should I be handling this? 我应该如何处理呢?

Thanks! 谢谢!

Edit: Fixed syntax to match what I actually have. 编辑:固定语法,以匹配我实际拥有的。 Here are the errors that I get when this syntax http://example.com/wp-json/posts?type[]=book&type[]=movie&filter[posts_per_page]=10 is used: 这是使用以下语法http://example.com/wp-json/posts?type[]=book&type[]=movie&filter[posts_per_page]=10时出现的错误:

Warning: urlencode() expects parameter 1 to be string, array given in /home/newbreak/public_html/wp-includes/formatting.php on line 4128 警告:urlencode()期望参数1为字符串,在第4128行的/home/newbreak/public_html/wp-includes/formatting.php中给出的数组

Warning: Cannot modify header information - headers already sent by (output started at /home/newbreak/public_html/wp-includes/formatting.php:4128) in /home/newbreak/public_html/wp-content/plugins/json-rest-api/lib/class-wp-json-server.php on line 587 警告:无法修改标头信息-/ home / newbreak / public_html / wp-content / plugins / json-rest-中已经发送过的标头(输出始于/home/newbreak/public_html/wp-includes/formatting.php:4128)第587行的api / lib / class-wp-json-server.php

Warning: Cannot modify header information - headers already sent by (output started at /home/newbreak/public_html/wp-includes/formatting.php:4128) in /home/newbreak/public_html/wp-content/plugins/json-rest-api/lib/class-wp-json-server.php on line 587 警告:无法修改标头信息-/ home / newbreak / public_html / wp-content / plugins / json-rest-中已经发送过的标头(输出始于/home/newbreak/public_html/wp-includes/formatting.php:4128)第587行的api / lib / class-wp-json-server.php

Warning: Cannot modify header information - headers already sent by (output started at /home/newbreak/public_html/wp-includes/formatting.php:4128) in /home/newbreak/public_html/wp-content/plugins/json-rest-api/lib/class-wp-json-server.php on line 587 警告:无法修改标头信息-/ home / newbreak / public_html / wp-content / plugins / json-rest-中已经发送过的标头(输出始于/home/newbreak/public_html/wp-includes/formatting.php:4128)第587行的api / lib / class-wp-json-server.php

Warning: Cannot modify header information - headers already sent by (output started at /home/newbreak/public_html/wp-includes/formatting.php:4128) in /home/newbreak/public_html/wp-content/plugins/json-rest-api/lib/class-wp-json-server.php on line 587 警告:无法修改标头信息-/ home / newbreak / public_html / wp-content / plugins / json-rest-中已经发送过的标头(输出始于/home/newbreak/public_html/wp-includes/formatting.php:4128)第587行的api / lib / class-wp-json-server.php

Warning: Cannot modify header information - headers already sent by (output started at /home/newbreak/public_html/wp-includes/formatting.php:4128) in /home/newbreak/public_html/wp-content/plugins/json-rest-api/lib/class-wp-json-server.php on line 587 警告:无法修改标头信息-/ home / newbreak / public_html / wp-content / plugins / json-rest-中已经发送过的标头(输出始于/home/newbreak/public_html/wp-includes/formatting.php:4128)第587行的api / lib / class-wp-json-server.php

Warning: Cannot modify header information - headers already sent by (output started at /home/newbreak/public_html/wp-includes/formatting.php:4128) in /home/newbreak/public_html/wp-content/plugins/json-rest-api/lib/class-wp-json-server.php on line 587 警告:无法修改标头信息-/ home / newbreak / public_html / wp-content / plugins / json-rest-中已经发送过的标头(输出始于/home/newbreak/public_html/wp-includes/formatting.php:4128)第587行的api / lib / class-wp-json-server.php

I only get the errors when I send the type as an array type[] . 当我将类型作为数组type[]发送时,我只会收到错误。

You can also try registering an additional endpoint so you don't have to add as many query arguments in each request. 您也可以尝试注册其他端点,这样就不必在每个请求中添加尽可能多的查询参数。 I'm using the latest version of the WP REST API plugin. 我正在使用最新版本的WP REST API插件。

So for a custom content type called "movies", you could have an endpoint like wp-json/wp/v2/movies 因此,对于名为“电影”的自定义内容类型,您可以拥有一个端点,如wp-json/wp/v2/movies

add_action('rest_api_init', 'register_movies');

function register_movies() {
  $movies_custom_fields = array(
    'director',
    'genre'
  );
  foreach ($movies_custom_fields as $key) {
     register_rest_field('movies', $key, array(
     'schema'          => null,
     'get_callback'    => 'get_movies_data',
   ));
  }
}

function get_movies_data( $object, $field_name, $request ) {
   return get_post_meta( $object[ 'id' ], $field_name, true );
}

More documentation on adding custom endpoints for different content types at the WP REST API V2 documentation site. 有关在WP REST API V2文档站点上添加针对不同内容类型的自定义终结点的更多文档。

We had a similar requirement, where we only had a slug and need to query / find the post/page/article data. 我们有一个类似的要求,即我们只有一个,需要查询/查找帖子/页面/文章数据。

We have build a wordpress api v2 plugin to query multiple post types. 我们已经构建了一个wordpress api v2插件来查询多种帖子类型。

Here is the plugin code https://github.com/elevati/wp-api-multiple-posttype 这是插件代码https://github.com/elevati/wp-api-multiple-posttype

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

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