简体   繁体   English

如何访问/发布 WordPress JSON REST ZDB974238714CA8DE634A7CE1DZ03 和过滤器?

[英]How to access /posts WordPress JSON REST API and filter by post_type?

I have a number of posts in the table wp_3_posts with the post_type "people".我在 wp_3_posts 表中有许多帖子,post_type 为“人”。 This post_type is created using the Admin Columns plugin.这个 post_type 是使用 Admin Columns 插件创建的。

How do I retrieve these?我如何检索这些?

The documentation for Posts offers filtering by categories or tags but post_type is neither of these. Posts 的文档提供按类别或标签进行过滤,但 post_type 都不是。

https://developer.wordpress.org/rest-api/reference/posts/ https://developer.wordpress.org/rest-api/reference/posts/

Thank you:]谢谢:]

If I got your question the right way, you want to get posts of a custom post type with REST API.如果我以正确的方式解决了您的问题,您希望使用 REST API 获得自定义帖子类型的帖子。

You have to set show_in_rest and public in the arguments when you create the custom post type in wordpress.当您在 wordpress 中创建自定义帖子类型时,您必须在 arguments 中设置show_in_restpublic

add_action( 'init', 'my_cpt' );
function my_cpt() {
    $args = array(
      'public'       => true,
      'show_in_rest' => true,
      'label'        => 'My Custom Post'
    );
    register_post_type( 'mycustompost', $args );
}

More about this: https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-rest-api-support-for-custom-content-types/有关此的更多信息: https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-rest-api-support-for-custom-content-types/

With that having set, you can get the posts of post type with using the right parameters in the url.设置完成后,您可以使用 url 中的正确参数获取帖子类型的帖子。

So if you just want to get posts, you can use:因此,如果您只想获取帖子,可以使用:

https://yoursite.com/wp-json/wp/v2/mycustompost?per_page=10

I would suggest setting the per_page to have control if you are getting a lot of posts.如果您收到很多帖子,我建议设置per_page以进行控制。

You can also have access to more data without additional HTTP requests using _embed您还可以使用_embed访问更多数据,而无需额外的 HTTP 请求

https://yoursite.com/wp-json/wp/v2/mycustompost?per_page=10&_embed=wp:term,wp:featuredmedia

For example, with this you get taxonomy terms and urls of different featured image sizes.例如,通过这个,您可以获得不同特色图像大小的分类术语和 url。


So you do not need to get all posts (and post types) of your website and then filter by post type, but just get posts of this post type instead.因此,您不需要获取您网站的所有帖子(和帖子类型),然后按帖子类型进行过滤,而只需获取此帖子类型的帖子即可。 You can than do more filtering using global parameters:您可以使用全局参数进行更多过滤:

https://developer.wordpress.org/rest-api/using-the-rest-api/global-parameters/ https://developer.wordpress.org/rest-api/using-the-rest-api/global-parameters/

With VueJS (in my opinion better performance) this would look something like:使用 VueJS(在我看来更好的性能)这看起来像:

fetch("https://yoursite.com/wp-json/wp/v2/mycustompost?per_page=10")
        .then(response => response.json())
        .then((data => {
            this.mycustompost = data;
        }))

Or if using standard javascript something like:或者,如果使用标准 javascript 类似:

let state = {
      posts: [],
      baseUrl: 'https://yoursite.com/wp-json/wp/v2/mycustompost',
      perPage: '?per_page=10',
      wpFetchHeaders: {
        headers: {
          'Access-Control-Allow-Origin': '*',
          'Access-Control-Expose-Headers': 'x-wp-total'
        }
      }
    }

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

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