moxie-lean/wp-endpoints-collection
该包已废弃,不再维护。未建议替代包。
查询WordPress站点并获取与您的请求相关联的数据集合的JSON响应。
1.2.0
2016-06-13 18:52 UTC
Requires
- php: >=5.4
- moxie-lean/wp-endpoint: 1.*.*
- moxie-lean/wp-utils: 1.*.*
Requires (Dev)
README
该包已过时。LEAN现在使用WordPress REST API插件
查询WordPress站点并获取与您的请求相关联的数据集合的JSON响应。
入门指南
通过终端使用composer安装此包是最简单的方法
composer require moxie-lean/wp-endpoints-collection
或者,在您的composer.json
文件中添加以下行
"require": { "moxie-lean/wp-endpoints-collection": "dev-master" }
这将从Packagist站点下载文件,并将您设置到位于仓库master分支的最新版本。
之后,您可以通过包含autoload.php
文件来在对象创建时自动加载类。
include '/vendor/autoload.php'; \Lean\Endpoints\Collection::init();
功能
- 避免查询处于草稿、私有或其他非发布状态的帖子。
- 避免查询受密码保护的帖子。
默认情况下只返回帖子集合,但也可以指定返回任何帖子类型或多个帖子类型的集合。
用法。
默认URL是
/wp-json/leean/v1/collection
```
By default the collection is the list of posts, you can use most
of the WP_Query params in order to update your results, for example by
default uses setting that specifies the number of reading post on the
settings page.
## Request examples
Get only the latest 3 posts.
```json
wp-json/leean/v1/collection?posts_per_page=3
```
How about get the latest 3 posts that belongs to the author with the ID
1.
```json
wp-json/leean/v1/collection?posts_per_page=3&author=1
```
Or just get the posts that belong to the author with the ID 1.
```json
wp-json/leean/v1/collection?author=1
```
Get the all the posts ordered by ID from lowest to highest values.
```json
wp-json/leean/v1/collection?orderby=ID&order=ASC
```
Get all the posts and pages published on the site.
```json
wp-json/leean/v1/collection?post_type[]=post&post_type[]=page
```
Get the first and second page of the blog section.
```json
wp-json/leean/v1/collection
wp-json/leean/v1/collection?paged=2
```
Get all the posts that has the category ID 2
```json
wp-json/leean/v1/collection?cat=2
```
## Filters
There are filters that can be used on this particular endpoint.
`ln_endpoints_collection_args`. This filter allow you to overwrite the
default arguments used to query inside of the collection so you can
overwrite the default values used on the `WP_Query` before executed.
`ln_endpoints_collection_data`. This filter allow you to overwrite the
data after processing and before is sending it to the client it has 1
parameter that can be used on the filter:
- `$data` The original data created by the endpoint, this is an array
with all the data, for example if you want to return an empty array
if the data has zero items.
- `$request` An array with the arguments used to create the request.
```php
add_filter('ln_endpoints_collection_data', function( $data ){
if ( isset( $data['pagination']['items'] ) && $data['pagination']['items'] === 0 ) {
return [];
} else {
return $data;
}
});
```
`ln_endpoints_collection_item`. Allows you to easily customise the output of each post:
```php
add_filter( 'ln_endpoints_collection_item', function($item, $the_post) {
if ( $the_post->ID === 1 ) {
return [
'message' => 'Nothing here',
];
} else {
return $item;
}
}, 10, 2);;
```