I using angular to display posts with the WP Rest Api. However the search will only filter through the first 99 posts since that is the max. Is there anyway to yield all results?
Create a custom endpoint that utilizes a custom query. Something like this:
add_action( 'rest_api_init', function () {
register_rest_route( 'search', '/all', array(
'methods' => 'GET',
'callback' => 'get_all_posts',
));
});
Endpoint: yoursite.com/wp-json/search/all
function get_all_posts() {
$query = new WP_Query(
array( 'posts_per_page' => -1 )
);
return json_encode( $query );
}
Remember to clear the permalinks when registering new endpoints!
Getting posts is capped at 100 per page, but each request returns the header X-WP-TotalPages which gives you the number of total pages in your request.
So do a request for the next pages...
var url = your-route.example/wp-json/wp/v2/posts?per_page=100
$http.get(url).
success(function(response) {
var pagesNum = response.headers('X-WP-TotalPages')
for (var i = 2; i <= pagesNum; i++) {
$http.get(url+'&page='+i)
.success(function(response){
// do something with the next page data
})
}
});
...and so on, until you've gone through all the available pages.
Source: REST API Handbook
Related
I am stuck on creating custom endpoints. I followed this tutorial to create custom endpoints. But, it always returns 404.
https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/
I added code below to function.php:
add_action('rest_api_init', function() {
register_rest_route('awesome/v1', '/awesomeparams', array(
'methods' => 'GET',
'callback' => 'get_awesome_params',
'args' => array(),
'permission_callback' => function () {
return true;
}
));
});
function get_awesome_params( $data ) {
return "aaaa";
}
When I trying to access: http://www.my-domain.com/wp-json/awesome/v1/awesomeparams
It shows 404:
I also tried to flush the permalinks by going to WordPress admin -> Settings -> Permalinks and click save.
Can I get some help?
Thanks a lot!
The solution is to use the url below:
http://www.my-domain.com/index.php/wp-json/awesome/v1/awesomeparams
I'm using WP as a headless CMS with ACF en ACF-2-REST plugins. I've added categories to a post-type and when I make a GET call, it shows me all the information of a particular post including the categories, but only the ID's. If I want to match it, I have to do another call to Categories to get the information of that categories (name, parent etc).
How can I show that information instead of just the ID in a post call?
How the JSON looks now at the /activities call:
{
"id":111,
"date":"2020-01-18T15:39:27",
"date_gmt":"2020-01-18T15:39:27",
"guid":{"rendered":"https:\/\/url.be\/?post_type=activities&p=111"},
"modified":"2020-01-18T15:39:27",
"modified_gmt":"2020-01-18T15:39:27",
"slug":"walking-on-wood",
"status":"publish",
"type":"activities",
"link":"https:\/\/url.be\/activities\/walking-on-wood\/",
"title":{"rendered":"Walking on wood"},
"template":"",
"categories":[14,25,13,2,18,21,6,24],
"acf":{...}
}
What I want to show in the "categories" instead of just the numbers (from the categories call)
{
"id":3,
"count":1,
"description":"",
"link":"https:\/\/url.be\/category\/duration\/lower-than-30-min\/",
"name":"< 30 min.",
"slug":"lower-than-30-min",
"taxonomy":"category",
"parent":2,"meta":[],
"acf":[],
"_links":{"self":[{"href":"https:\/\/url.be\/wp-json\/wp\/v2\/categories\/3"}],
"collection":[{"href":"https:\/\/url.be\/wp-json\/wp\/v2\/categories"}],
"about":[{"href":"https:\/\/url.be\/wp-json\/wp\/v2\/taxonomies\/category"}],
"up":[{"embeddable":true,"href":"https:\/\/url.be\/wp-json\/wp\/v2\/categories\/2"}],
"wp:post_type":[{"href":"https:\/\/url.be\/wp-json\/wp\/v2\/posts?categories=3"},{"href":"https:\/\/url.be\/wp-json\/wp\/v2\/activities?categories=3"}],
"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}
}
Can't find any solution on the internet how I could manipulate the structure of that JSON with a custom function, would appreciate it a lot if someone point me to the right direction. Thanks!
As discussed in the comments section, a solution to this question is to use a custom endpoint method for the WP REST API and perform extra queries in there to get the data you need. This way, you can do all the data manipulation to get the perfect response, resulting in one REST call.
As taken from the official developer docs
Define an endpoint method and add some extra data
<?php
add_action( 'rest_api_init', function () {
register_rest_route( 'myplugin/v1', '/author/(?P<id>\d+)', array(
'methods' => 'GET',
'callback' => 'my_awesome_func', //note that this is the method it will fire
'args' => array(
'id' => array(
'validate_callback' => function($param, $request, $key) {
return is_numeric( $param );
}
),
),
) );
/**
* Grab latest post by an author along with its category!
*
* #param array $data Options for the function.
* #return array Post,
*/
function my_awesome_func( $data ) {
$posts = get_posts( array(
'author' => $data['id'],
) );
if(!empty($posts)) {
//Example of appending extra data
foreach($posts as $post) {
$category = wp_get_post_terms($post->ID, 'category');
$post['category'] = $category;
}
return $posts;
} else {
return new WP_Error( 'no_author', 'Invalid author', array( 'status' => 404 ) );
}
}
I use wordpress standard with the plugins "Advanced custom fields" and "custom_post_type ui". I created a post_type called deals and added some custom fields with it.
What I need to do now, is filter the results when accessing the rest api like this:
http://localhost:8000/wp-json/wp/v2/deals
Actually I only need the acf part of it. I dont care about the rest.
[{"id":29,"date":"2019-04-12T12:34:14","date_gmt":"2019-04-
12T12:34:14","guid":{"rendered":"http:\/\/localhost:8000\/?
post_type=deals&p=29"},"modified":"2019-04-
12T12:34:14","modified_gmt":"2019-04-12T12:34:14",
"slug":"test-title","status":"publish","type":"deals",
"link":"http:\/\/localhost:8000\/deal s\/test- title\/","template":"",
"meta":[],"tax-deals":[],"acf":{"title":"Title for Deals
Post","description":"","image":false,"date_start":"01.01.1970",
"date_end":"01.01.1970","category":"Kleidung"},"_links":{"self":
[{"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/deals\/29"}],
"collection":[{"href":"http:\/\/localhost:8000\/wp-
json\/wp\/v2\/deals"}],"about":[{"href":"http:\/\/localhost:8000\/wp-
json\/wp\/v2\/types\/deals"}],"wp:attachment":
[{"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/media?
parent=29"}],"wp:term":[{"taxonomy":"tax_deals","embeddable":true,
"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/tax-deals?
post=29"}],"curies":
[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}},
I have already tried using
http://localhost:8000/wp-json/wp/v2/deals?search=id
to get the id or something, but response is empty.
Also this didnt work:
http://localhost:8000/wp-json/wp/v2/deals?id=28
Again empty response.
To summarize: I need to filter my custom post type on my custom fields by the "acf" attribute shown in my response json. How does it work?
EDIT: I already installed "WP REST Filter" but still dont know how to do it.
I suggest you to create a new API where you can customize the output. Take advantage of wordpress function register_rest_route() using this you can create an API from CPT and ACF in one ajax url. And you do not need to install anything.
Check how I get my instructor CPT and mycheckbox ACF.
// your ajaxurl will be: http://localhost/yoursite/wp-json/custom/v2/instructor/
add_action( 'rest_api_init', function () {
register_rest_route( 'custom/v2', '/instructor', array(
'methods' => 'GET',
'callback' => 'instructor_json_query',
));
});
// the callback function
function instructor_json_query(){
// args to get the instructor
$args = array(
'post_type' => 'instructor',
'posts_per_page' => -1,
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => 'mycheckbox', // your acf key
'compare' => '=',
'value' => '1' // your acf value
)
)
);
$posts = get_posts($args);
// check if $post is empty
if ( empty( $posts ) ) {
return null;
}
// Store data inside $ins_data
$ins_data = array();
$i = 0;
foreach ( $posts as $post ) {
$ins_data[] = array( // you can ad anything here and as many as you want
'id' => $posts[$i]->ID,
'slug' => $posts[$i]->post_name,
'name' => $posts[$i]->post_title,
'imgurl' => get_the_post_thumbnail_url( $posts[$i]->ID, 'medium' ),
);
$i++;
}
// Returned Data
return $ins_data;
}
Then, you can use the link: http://localhost/yoursite/wp-json/custom/v2/instructor/ in your ajax url.
I have created a Shopping Cart Website with Wordpress and Woocommerce Plugin. Now I want to access these products from my Ionic App.
Now, whenever I try to access these endpoints using Woo REST API. It gives the following Errors.
http://localhost:8888/myshop/wp-json/wc/v3/products/
{
code: "rest_no_route",
message: "No route was found matching the URL and request method",
data: {
status: 404
}
}
Please check and verify WooCommerce REST API option is added under,
www.mydomain.com/wp-admin/admin.php?page=wc-settings&tab=advanced§ion=keys
For making custom API end points, this snippet can be used.
add_action('rest_api_init', 'init_my_rest_api');
function init_my_rest_api() {
register_rest_route('myaction/v1', '/getdetails/', array(
'methods' => 'POST',
'callback' => 'gather_details',
));
}
http://localhost:8888/wp-json/wc/store/products
Try this endpoint for list products
My Solution,
Step 1:
Create a page:
<?php
/*
Template Name: Products API
*/
$productlist = array();
$vnfaster = new WP_Query(array(
'post_type'=>'product',
'post_status'=>'publish',
'orderby' => 'ID',
'order' => 'DESC',
));
while ($vnfaster->have_posts()) : $vnfaster->the_post();
$productlist[] = array( 'id' => $post->ID, 'title' => $post->post_title );
endwhile ; wp_reset_query();
echo json_encode( $productlist );
?>
Step 2:
I have a Page:
http://localhost:8888/myshop/products-api
Output:
{
id: "1",
title: "Product 1",
...
}
Based on a quick research, WordPress REST API should be enabled after v4.7 (https://v2.wp-api.org/), however, I cannot access the REST API for my existing or new user by simply appending {name}.wordpress.com/wp-json, ie. https://steventsaotest.wordpress.com/wp-json does not return the expected JSON. Whereas sites like http://www.tribunemedia.com/wp-json works exactly as I wish, as well as my WordPress instance on Digital Ocean.
How can a user whose blog is registered with wordpress.com enable their REST API?
add_action( 'rest_api_init', function () {
$namespace = 'aroma_api';
register_rest_route( $namespace, '/get_signin', array (
'methods' => 'POST',
'callback' => 'get_singnin_data',
));
} );
function get_singnin_data($data)
{
global $wpdb;
$resultdata = array();
$resultdata['message'] = 'Login SuccessFull';
return $resultdata;
}