I want to search posts by categories in the WP-API.
I know I can search posts by the attribute categories or filter[cat].
But the posts contains more than one category.
I tried to search like this:
{host}/wp-json/wp/v2/posts?categories=69&filter[cat]=[228,246,237]&per_page=50
or
{host}/wp-json/wp/v2/posts?categories=69&filter[cat]=228&filter[cat]=246&filter[cat]=237&per_page=50
or
{host}/wp-json/wp/v2/posts?categories=69&categories=246&categories=237&categories=228
This didn't work for me. It caused the search to look for the last attribute.
Any ideas?
This is the structure of Json response
{
"id": 9333,
"date": "2016-08-02T14:17:01",
"date_gmt": "2016-08-02T12:17:01",
"guid": {
"rendered": "{post}/?p=9333"
},
"modified": "2016-08-03T08:50:35",
"modified_gmt": "2016-08-03T06:50:35",
"slug": "{post}",
"type": "post",
"link": "{host}/{post}/",
"title": {
"rendered": "{post}"
},
"content": {
"rendered": "{post}"
},
"excerpt": {
"rendered": "{post}"
},
"author": 3,
"featured_media": 0,
"comment_status": "closed",
"ping_status": "closed",
"sticky": false,
"format": "standard",
"categories": [
228,
237,
207,
217,
246,
231,
69,
221,
270,
244
],
"tags": [],
"_links": []
}
Thanks!
If you want to get posts from multiple categories, there are few solutions, depending from your needs.
In case when you want to get posts from category with ID = 1 OR category with ID = 2 use the following URL:
http://localhost/lifelog/wp-json/wp/v2/posts?filter[cat]=1,2
or:
http://localhost/lifelog/wp-json/wp/v2/posts?categories=1,2
For case when you want to get posts from category with ID = 1 AND category with ID = 2 you can use:
http://localhost/lifelog/wp-json/wp/v2/posts?filter[category__and][]=1&filter[category__and][]=2
BUT - some filter values used in the filter array needs authenticated user with edit_posts privileges.
Fortunately, there is a more simple solution - WordPress supports links like:
http://example.com/category/test1+test2/
Under above URL you will get list of posts which are assigned to the test1 AND test2 categories. And in the REST API you can achieve the same behaviour with the following URL:
http://localhost/lifelog/wp-json/wp/v2/posts?filter[category_name]=test1%2Btest2
Please remember that you have to replace the + sign into %2B.
Related
I am building an eShop for educational purposes and I need to handle the orders from a user. A user has a basket which is getting filled with products. If he decides buy another product I have insert a document into the existing collection of the card
Current MongoDB collection:
{
"_id": {
"$oid": "61f3d79c921000006000547d"
},
"username": "mike",
"products": {[
"number": "3",
"name": "Honduras",
"price": 7,
"stock": 10,
]},
"status": "UNPAID"
}
By adding another product, needs to be inserted in the existing collection in the field of products.
Expected to look like:
{
"_id": {
"$oid": "61f3d79c921000006000547d"
},
"username": "mike",
"products": {[
"number": "3",
"name": "Honduras",
"price": 7,
"stock": 10,
], [
"number": "4",
"name": "India",
"price": 10,
"stock": 11,
]},
"status": "UNPAID"
}
I am using PHP for the back end operations. The script that I wrote it is simple. I am searching if orders with user's username exist. If they exist then I lock the current order and make the operations needed.
I think that I am missing something in syntax of the update for the purpose described above:
PHP script:
if (isset($_POST['add'])){
// Ordered by name from URL
$username = $_GET['username'];
// Product info
$name = $_POST['add'];
// Finds the product selected from products
$product = $collection -> findOne(array("name" => "$name"));
// Serialize product to be added.
$json = MongoDB\BSON\toJSON(MongoDB\BSON\fromPHP($product));
// Searching for order from certain user
$collection = $db -> orders;
$exists = $collection -> findOne(array("username" => "$username"));
if (!is_null($exists)){
// The problem is here (maybe?)
$exists->updateOne(
array("products" => {}),array('$set'=>$json);
);
}
Any help and suggestions would be really appreciated!
Well, you need to use something like below
db.collection.update(
{find Condition},
{$push: {products : {key: value, key2: value 2}}
)
Here the catch is push. It adds an element to array. Here the element is an object.
Unfortunately I'm working with wordpress and woocommerce, what I'm trying to accomplish is to insert products and relative translations using the woocommerce REST API, here's the scenario:
First, i create a simple product
{
"name": "Dummy",
"type": "variable",
"attributes": [
{
"id": 2,
"name": "Color",
"position": 1,
"variation": "true",
"options": [
"Bianco",
"Nero"
]
},
{
"id": 3,
"name": "Size",
"position": 2,
"variation": "true",
"options": [
"A",
"B",
"C"
]
}
]
}
This is a variable product, so I need to create its variations as well
The product created return its id, let's say 10
Then I create several product variations like this:
{
"regular_price": "10.10",
"stock_quantity": 10,
"attributes": [
{
"id": 2,
"name": "Color",
"option": "Bianco"
},
{
"id": 3,
"name": "Size",
"option": "A"
}
]
}
So I have a product with variations, now i want to create a translation of the product with wpml, according to wmpl rest api specifications, I create a product translation like this:
{
"name": "Dummy en",
"type": "variable",
"lang": "en",
"translation_of": 10
}
I specified the language en, the main language is it
I included translation_of with the id of the original product
What happens is that the english product is created, but without variations
What I have tried:
Several issues similar to this are solved by the troubleshooting page of wpml, where I can generate missing variations but I need this to be done automatically, products are created automatically, so relative translations too must be
If I enter product page from wordpress back end and I press the update button, variations are created and adjusted, this word for the original product and the variation, but again, I need this to be done automatically
I tried to modify and insert the translation in almost all ways, adding/removing sku, id, attributes, categories ecc.
Any hints? Thanks
I have a custom post type called "products" and it has a taxonomy called "domain".
I am using the WP Rest API and AngularJS 1.5.1. I am using a service to get the product posts. This part works fine.
getProducts: function () {
return $http.get('URL/wp-json/wp/v2/products').then(function (result) {
return result.data;
});
}
This returns an array of products, each being (partial):
{
"id": 29,
"date": "2017-10-09T16:21:56",
"date_gmt": "2017-10-09T16:21:56",
"guid": {
"rendered": "URL/?post_type=product&p=29"
},
"modified": "2017-10-09T19:58:32",
"modified_gmt": "2017-10-09T19:58:32",
"slug": "product-name",
"status": "publish",
"type": "product",
"link": "URL/product/product-name/",
"title": {
"rendered": "product name"
},
"content": {
"rendered": "some content",
"protected": false
},
"featured_media": 30,
"template": "",
"domain": [
2
],
...
}
As you can see, my taxonomy term "domain" is represented by a number, "2" in this case.
However, Using Postman, if I do a GET as described here:
URL/wp-json/wp/v2/products?filter[domain]=2
I still get all my products back, not just the ones with domain=2 as I expected.
What am I missing here?
It seems WP removed the filter parameter in v4.7. You can get a plugin to add the feature back in here.
I just merged that function into my functions.php and it works like this:
/wp-json/wp/v2/products?filter[taxonomy_name]&filter[term]=taxonomy-slug
We are using the Podio PHP library to create forms for our users. These forms load their data from Podio trough the Podio PHP library. A form is linked to a specific Podio Item ID for this.
With this ID we get the fields and their possible values. Most of them are Category fields. We get the values for this trough:
<?php
$item_data = PodioItem::get_basic( $podio_item_id );
$car_types = $item_data->fields["car-type"]->config['settings']['options'];
var_dump( $car_types );
?>
But the curious thing is, if the item in Podio has no value selected for this category, getting the options fail. It's empty. It doesn't exists.
How do we populate a form with values even if the item has no options selected for it?
Podio doesn't store empty value, it's empty, it doesn't exist, so there is nothing to store. In fact, what you are probably looking for is Application definition, which has list fields. Then category field has config with list of possible values and id's and colors.
Like this:
"fields": [
{
"status": "active",
"type": "category",
"field_id": 81772,
"label": "Status",
"config": {
"required": true,
"label": "Status",
.....some other config values .....
"settings": {
"multiple": false,
"options": [
{
"status": "deleted", <= example of deleted category option
"text": "Not groomed",
"id": 13,
"color": "DCEBD8"
},
{
"status": "active", <= example of active category option
"text": "Open",
"id": 1,
"color": "FFD5C2"
},
.... <= other category options goes here
You can read more about Apps here: https://developers.podio.com/doc/applications/get-app-22349
I have a query in aws cloudsearch. I did the following things
1) Created domain
2) uploaded the data & created indexing
I have data fields like : user_id, user_name, user_details, etc
My objective is to get the grouped/distinct data of particular field & its total count. In Cloudsearch Group by / Distinct key words not supported. So, I went through the cloudsearch documentation & done it by adding facet.user_id={} in my query string.
But I need user_name field data along with user_id and count.** Please update me regarding this.
Here is my full query : ?q="Tamil Selvan"&facet.user_id={}
Here is my query result :
{
"status": {
"rid": "isTcmOYp+AEKhpbc",
"time-ms": 6
},
"hits": {
"found": 986,
"start": 0,
"hit": []
},
"facets": {
"user_id": {
"buckets": [{
"value": "5",
"count": 213
}, {
"value": "182",
"count": 197
}]
}
}
}
My expected result :
{
"status": {
"rid": "isTcmOYp+AEKhpbc",
"time-ms": 6
},
"hits": {
"found": 986,
"start": 0,
"hit": []
},
"facets": {
"user_id": {
"buckets": [{
"value": "5",
"user_name":"Tamil Selvan",
"count": 213
}, {
"value": "182",
"user_name":"Tamil Selvi",
"count": 197
}]
}
}
}
The proper solution would be to look up the user_names for the user_id facet values from your datastore (which CloudSearch is not, or at least should not be).
CloudSearch is a search solution; you shouldn't be trying to ask it which user_name belongs to some user_id, as that's a question for your data store.