How do you get Posts by multiple meta_keys and meta_values with the Rest API V2? - metadata

I'm trying to get my Posts by multiple meta_keys and meta_values. How do I accomplish this? The URL should be looking like this:
"/posts?meta_key=Example&meta_value=Example2&meta_key=Example3&meta_value=Example4"
I tried to find a solution for this quite a while now, but couldn't find anything as most things were outdated.

I found a solution for my Problem. What i did is quite simple. I just setted up a new Query Params.
if (!function_exists('post_meta_rest_api_request')) :
function post_meta_rest_api_request($argu, $request)
{
$argu += array(
'meta_key' => $request['meta_key'],
'meta_value' => $request['meta_value'],
'meta_query' => $request['meta_query'] == 1 ? array(
array(
"key" => "key1",
"value" => "value1"
),
array(
"key" => "key2",
"value" => "value2"
)
) : $request['meta_query']
);
return $argu;
}
add_filter('rest_custom_query', 'post_meta_rest_api_request', 99, 2);
endif;
So if you now make an API call like: wordpress/wp-json/wp/v2/customType?meta_query=1
the API Request will take in your custom Query Params. Otherwise it will just take in the normal Meta Query Request.
For references look here: WordPress Rest-API Requests

Related

Find query with and operator in PHP

Hi i am working on backend of web application & want to find the documents from mongodb database that contain key active_status with value set to both 1 & 2. With mongodb PHP i am confused of how to find with both parameters in single query.
My query was this:
$mongoDb = MongoDbConnector::getCollection("endusers");
$endUserData = $mongoDb->find(array('active_status' => 1, '$and' => array('active_status' => 2)));
I have to fetch the users whose active_status should be 1 & 2. The above query doesnt seems to work. What is the right one for that?
Thanks on advance for quick response.
You have $and the wrong way around. Both arguments need to be included:
$endUserData = $mongoDb->find(array(
'$and' => array(
array( 'active_status' => 1 )
array( 'active_status' => 2 )
)
));
And since that would only make sense when looking for both elements within an array element, then you should instead use $all, which is shorter syntax:
$endUserData = $mongoDb->find(array(
'active_status' => array( '$all' => array(1,2) )
));
I should add that unless you intend to match a document like this:
{ "active_status" => [1,2] }
The you do not in fact want $and at all, but rather you want $or or better yet $in for multiple possible values on the same field:
$endUserData = $mongoDb->find(array(
'active_status' => array( '$in' => array(1,2) )
));
This matches documents like this:
{ "active_status": 1 },
{ "active_status": 2 }

Find query MongoDB using CakePHP

The following is the json array I have in a collection called claims in MongoDB.
{
"xmllisting_id": "537f371fb2e380922fff0e2c",
"pharmacyfiles_id": "537f3402b2e380732aa6032d",
"claim": {
"MemberID": "097110330047532601",
"PatientShare": "0",
},
"modified": ISODate("2014-05-23T13:12:17.191Z"),
"created": ISODate("2014-05-23T13:12:17.192Z")
}
I need to find all claims with a specified MemberID.I have tried the following in CakePHP without any success.
$claims = $claimobj->find(
'all',
array(
'conditions' => array(
'claim' => array('MemberID' => '097110330047532601')
)
)
);
How can I do it?
Finding "nested" details in MongoDB usually requires "dot notation". Otherwise you are actually asking for an object that has "exactly" the key and "only" the key you are specifying to match. Which of course it does not, as there is more information there:
$claims = $claimobj->find(
'all',
array(
'conditions' => array(
'claim.MemberID' => '097110330047532601'
)
)
);
So the path is "claim.MemberID" and not 'claim' => array('MemberID' => '097110330047532601' ) as you have written.

With cakephp2.3 and mongoDB how to add array in a document?

how can I work with lists within the mongoDB?
For example, I have a document (class), and a list of students in this class, ie a subdocument (students).
What do not you, is to add more students to a class.
in Model:
public function salvar($name,$idClass){
$new = array(
"_id" => $idClass,
"student"=> array(
'idStudent' => new MongoId(),
'name' => $name));
return $this->save($new);
}
But when you add a new student, he is not working that way.
How to perform a $push update
The answer you've provided clarifies what you've been trying to do. However, the solution is a little convoluted. To perform a $push update with MongoDB, simply specify you want to perform a push update:
$this->Foo->save(array(
'_id' => $id,
'$push' => array(
"images" => array(
'id' => new MongoId(),
'name' => $name,
'size' => $size
)
)
);
You can use the mongoNoSetOperator property to achieve the same thing but
It's indirect, therefore not obvious
With multiple calls to save in the same request it can be confusing if it's not reset to $set if it's modified
It prevents using multiple operators in a single call.
There's more information about using different update operators in the documentation.
the answer to the question was:
public function salvar($name,$size,$id){
$this->mongoNoSetOperator = '$push';
// fixed array data structure
$susp = array(
"_id" => $id,
"images"=> array(
array(
'id' => new MongoId(),
'name' => $name,
'size' => $size
)
)
);
return $this->save($susp);
}
Note: Thank Garamon from github, by clear and objective answers.

MongoDB (via Fuelphp): Adding entries on an Array

I would appreciate the help here. For the purpose of this discussion I have an example here (I will use paste bin for the codes):
http://pastebin.com/VPuyKn6W
I am trying to produce this output:
http://pastebin.com/4iMLacRu
I understand that I need to use $push to make this work. But upon testing, it doesn't seem to do anything. I am following the instructions as prescribed in the docs, but instead of using $Id, I am using user_id for finding the document in the collection. Here is my model:
http://pastebin.com/QB94tbZn
Am I misunderstanding something, or I am not using the $push operator properly, or something to do on how I created the document?
After walking outside, I finally got my answer.
public static function create_mongo()
{
$data = array(
'user_id' => '123895',
'First_Name' => 'John',
'Last_name' => 'Doe',
'sites' => array(
array(
'title' => 'Sankaku Complex',
'site' => 'http://sankakucomples.com'
)
)
);
$db = Fuel\Core\Mongo_Db::instance();
$db->insert('test_collection',$data);
}
sites should be an array carrying an array variable.

Symfony2 mongodb bundle document one-to-many forms

I have a problem using forms in Symfony 2 with mongoDB documents.
I'm trying to have a form that will represent my first document (Post) with a relation oneToMany to Tags (reference)
The relation is declared like this :
/**
* #Assert\Collection
* #MongoDB\ReferenceMany(targetDocument="Acme\ManagerBundle\Document\Tags")
*/
protected $tags;
A tag has an Id and a Name.
I have tried a lot of things to make it work
$form = $this->createFormBuilder($tag)->add('tags', 'choice', array('choices' => $tags, 'multiple' => true, 'expanded' => true, 'empty_value' => true, ))
The form show the choices but once its submited the form is not valid and keep showing thhis error :
"The fields "0", "1", "2" were not expected"
I've also tried this : symfony2 form choice and mongodb
But the use of it is kinda confusing
UPDATE
This is what i get after the post is submited :
object(Doctrine\Common\Collections\ArrayCollection)#795 (1) {
["_elements":"Doctrine\Common\Collections\ArrayCollection":private]=>
array(2) {
[0]=>
object(Acme\ManagerBundle\Document\Tags)#723 (2) {
["id":protected]=>
string(24) "4f7a0eb1ecd111b99c3d2f25"
["name":protected]=>
string(6) "Fruits"
}
[1]=>
object(Acme\ManagerBundle\Document\Tags)#720 (2) {
["id":protected]=>
string(24) "4f7a0ec7ecd111b99c3d2f26"
["name":protected]=>
string(10) "Vegetables"
}
}
}
So now i understand why i have "The fields "0", "1", "2" were not expected" but i dont understand why Symfony doesn't process it.
I've been looking a the possible bundles but nothing
I have no idea how to have a nice form that will hydrate my object and the related objects,
does anyone has a solution for this issue or other idea to solve this?
Thanks a bunch !
Without seeing the data involved I can only make a best guess here.
It feels like your line of code should look something like.
$tags = $post->getTags();
$fixedTags = array();
foreach ($tags as $tag) {
$fixedTags[$tag->getId()] = $tag->getName();
}
$form = $this->createFormBuilder($post)
->add(
'tags',
'choice',
array(
'choices' => $fixedTags,
'multiple' => true,
'expanded' => true,
'empty_value' => true
)
);
Now I think whats happening is you are getting your $tags data in a form like this.
array(0 => (Object)Tag, 1 => (Object)Tag, 2 => (Object)Tag)
Where as what you really want is probably like this.
array('topic1' => 'Topic 1', 'topic2' => 'Topic 2', 'topic3' => 'Topic 3')
If you this isn't the case, reply with some data output and I'm sure we'll be able to help some more.
A choice field won't save by default (although you could manually do it on form submission). You need to look into the document type which is admittedly not documented well but it's essentially the same type as entity here.
I didn't see this was from 3 years ago! Well, it's here in case others find this page I guess.