Skip to content

Commit b6759f6

Browse files
committed
Update Instructions
1 parent 1a162af commit b6759f6

File tree

1 file changed

+118
-8
lines changed

1 file changed

+118
-8
lines changed

README.md

Lines changed: 118 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,11 @@ echo '</pre>';
3333

3434
Response Success:
3535

36-
Body = All respon from Wordpress api
37-
Total = Total Item from X-WP-TOTAL
38-
TotalPages = Total Page From X-WP-TOTALPAGE
36+
status = status name
37+
code = status Code
38+
data = callback data from wordpress
39+
X-WP-TOTAL = count all post on current result
40+
X-WP-TOTAL-PAGE = count all page on current result
3941

4042
```Array
4143
Array
@@ -57,16 +59,16 @@ Array
5759

5860
Response Error:
5961

60-
Body = All respon from Wordpress api
61-
Total = Total Item from X-WP-TOTAL
62-
TotalPages = Total Page From X-WP-TOTALPAGE
62+
status = status name
63+
code = status Code
64+
error = error message
6365

6466
```Array
6567
Array
6668
(
6769
68-
[status] => success
69-
[code] => 200
70+
[status] => failed
71+
[code] => 400
7072
[error] => Array(
7173
7274
message =>
@@ -77,6 +79,114 @@ Array
7779
)
7880
```
7981

82+
## Addon Custom Route
83+
Insert On Functions Your Theme
84+
```php
85+
function custom_api_get_all_posts()
86+
{
87+
register_rest_route('custom/v1', '/all-posts', array(
88+
'methods' => WP_REST_Server::READABLE,
89+
'callback' => 'custom_api_get_all_posts_callback'
90+
));
91+
}
92+
93+
// custom Route API
94+
function custom_api_get_all_posts_callback(WP_REST_Request $request)
95+
{
96+
97+
$posts_data = array();
98+
99+
$paged = $request->get_param('page');
100+
$paged = (isset($paged) || !(empty($paged))) ? $paged : 1;
101+
102+
$author = $request->get_param('author');
103+
$author = (isset($author) || !(empty($author))) ? $author : '';
104+
105+
$search = $request->get_param('search');
106+
$search = (isset($search) || !(empty($search))) ? $search : '';
107+
108+
$after = $request->get_param('after');
109+
$after = (isset($after) || !(empty($after))) ? $after : '';
110+
111+
$before = $request->get_param('before');
112+
$before = (isset($before) || !(empty($before))) ? $before : '';
113+
114+
115+
116+
117+
$query = new WP_Query(
118+
array(
119+
's' => $search,
120+
'author' => $author,
121+
'paged' => $paged,
122+
'post__not_in' => get_option('sticky_posts'),
123+
'posts_per_page' => 10,
124+
'post_type' => array('post'),
125+
'date_query' => array(
126+
//set date ranges with strings!
127+
'after' => $after,
128+
'before' => $before,
129+
130+
),
131+
'orderby' => 'date',
132+
'order' => 'DESC'
133+
134+
)
135+
);
136+
137+
// if no posts found return
138+
if (empty($query->posts)) {
139+
return new WP_Error('no_posts', __('No post found'), array('status' => 404));
140+
}
141+
142+
// set max number of pages and total num of posts
143+
$max_pages = $query->max_num_pages;
144+
$total = $query->found_posts;
145+
146+
$posts = $query->posts;
147+
148+
// prepare data for output
149+
$controller = new WP_REST_Posts_Controller('post');
150+
151+
foreach ($posts as $post) {
152+
153+
$response = $controller->prepare_item_for_response($post, $request);
154+
$post_data = $controller->prepare_response_for_collection($response);
155+
$post_thumbnail = (has_post_thumbnail($post_data['id'])) ? get_the_post_thumbnail_url($post_data['id']) : null;
156+
157+
$data[] = (object) array(
158+
'id' => $post_data['id'],
159+
'date' => $post->post_date,
160+
'author' => array(
161+
'id' => $post->post_author,
162+
'name' => get_the_author_meta('user_nicename', $post->post_author),
163+
),
164+
'type' => $post->post_type,
165+
'title' => $post->post_title,
166+
'url' => get_permalink($post_data['id']),
167+
'content' => $post_data['excerpt']['rendered'],
168+
'featured_img_src' => $post_thumbnail,
169+
'meta' => get_post_meta($post_data['id'], '', '')
170+
171+
);
172+
}
173+
174+
// set headers and return response
175+
$response = new WP_REST_Response($data, 200);
176+
177+
$response->header('X-WP-Total', $total);
178+
$response->header('X-WP-TotalPages', $max_pages);
179+
180+
return $response;
181+
}
182+
```
183+
184+
Parameter :
185+
- page = page your data
186+
- author = filter by author id or name
187+
- search = search post like search on API Post v2
188+
- after and Before = Filter date Range better use Format d-f-y on query string or string on backend logic
189+
80190
## Screenshoot
81191

82192
![Backend](https://raw.githubusercontent.com/reactmore/wordpress-rest-api-client/master/preview.jpg)

0 commit comments

Comments
 (0)