I have this weird functionality that is not working in wordpress.
I'm using woocomerce and I have to get some products based on some filters using the following dump code:
["tax_query"]=>
array(2) {
[0]=>
array(4) {
["taxonomy"]=>
string(11) "product_cat"
["field"]=>
string(4) "slug"
["terms"]=>
array(4) {
[0]=>
string(3) "running"
[1]=>
string(9) "hiking"
}
["operator"]=>
string(3) "AND"
}
[1]=>
array(4) {
["taxonomy"]=>
string(11) "product_cat"
["field"]=>
string(4) "slug"
["terms"]=>
array(2) {
[0]=>
string(5) "nike"
[1]=>
string(9) "adidas"
}
["operator"]=>
string(2) "OR"
}
}
For $args['tax_query'][0] I need to make AND logic. This means that the results must decrease if I select more checkboxes.
For $args['tax_query'][1] I need to make an OR logic.
The explanation for the above code is: Give me all the products that are for running but also for hiking and is produced by nike or adidas.
The following code returns 0 results. If I remove the ["operator"]=> 'AND' from $args['tax_query'][0] it will return all the results like it just pass the filter.
The final code is:
array(6) {
["posts_per_page"]=>
string(2) "12"
["post_status"]=>
string(7) "publish"
["post_type"]=>
string(7) "product"
["offset"]=>
string(1) "0"
["tax_query"]=>
array(2) {
[0]=>
array(4) {
["taxonomy"]=>
string(11) "product_cat"
["field"]=>
string(4) "slug"
["terms"]=>
array(3) {
[0]=>
string(3) "running"
[1]=>
string(9) "hiking"
}
["operator"]=>
string(3) "AND"
}
[1]=>
array(4) {
["taxonomy"]=>
string(11) "product_cat"
["field"]=>
string(4) "slug"
["terms"]=>
array(2) {
[0]=>
string(11) "nike"
[1]=>
string(34) "adidas"
}
["operator"]=>
string(2) "OR"
}
}
["meta_query"]=>
NULL
}
Any explanation or a fix for this?
Thank you.
You would need to use OR relation first, then AND
Modify the arguments as you need, this solution is just an idea.
Tested with POSTS
// WP_Query arguments
$args = array(
'post_type' => array( 'post' ),
'post_status' => array( 'publish' ),
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'category',
'terms' => array( 'Apple', 'Design' ),
'field' => 'name',
'operator' => 'AND',
),
array(
'relation' => 'AND',
array(
'taxonomy' => 'category',
'terms' => array('Photography', ' Android'),
'field' => 'name',
),
),
),
);
// The Query
$query = new WP_Query( $args );
foreach ($query->posts as $key => $post) {
echo '<pre>';print_r($post->post_title . ' - '. get_the_category_list(','));echo '</pre>';
}
Related
i want to make custom taxonomy that the terms from custom field , should i using get_post_meta instead of get_the_terms here is my code :
$post_guest = get_the_terms($postID, 'guest_no');
$items_guest[] = '';
$guest_array = '';
if ($post_guest!=''):
foreach ($post_guest as $item) {
$items_guest[] = $item->term_id;
}
$guest_array=array(
'taxonomy' => 'guest_no',
'field' => 'id',
'terms' => $items_guest
);
endif;
but my code result is like this :
array(3) {
["taxonomy"]=> string(8) "guest_no"
["field"]=> string(2) "id"
["terms"]=> array(3)
{
[0]=> string(0) ""
[1]=> NULL } }
i want to make the result like this
array(3) {
["taxonomy"]=> string(13) "property_area"
["field"]=> string(2) "id"
["terms"]=> array(2)
{
[0]=> string(0) ""
[1]=> int(90) } }
I am trying to filter a CPT to display fields by Taxonomy, I am currently using the following code:-
$args = array(
'posts_per_page'=> -1,
'post_type' => 'episode',
'order' => 'DESC',
//'meta_key' => $filter_key,
//'meta_value' => $filter,
'tax_query' => array(
array(
'taxonomy' => 'name',
'field' => 'make',
'terms' => array('Jaguar')
)
),
);
However, this is not returning any results.
This is what I am trying to filter:-
array(3) { [0]=> object(WP_Term)#7336 (10) { ["term_id"]=> int(25) ["name"]=> string(6) "Jaguar" ["slug"]=> string(6) "jaguar" ["term_group"]=> int(0) ["term_taxonomy_id"]=> int(25) ["taxonomy"]=> string(8) "post_tag" ["description"]=> string(0) "" ["parent"]=> int(0) ["count"]=> int(0) ["filter"]=> string(3) "raw" } [1]=> object(WP_Term)#7493 (10) { ["term_id"]=> int(24) ["name"]=> string(13) "Mercedes K100" ["slug"]=> string(13) "mercedes-k100" ["term_group"]=> int(0) ["term_taxonomy_id"]=> int(24) ["taxonomy"]=> string(8) "post_tag" ["description"]=> string(0) "" ["parent"]=> int(0) ["count"]=> int(0) ["filter"]=> string(3) "raw" } [2]=> object(WP_Term)#7492 (10) { ["term_id"]=> int(26) ["name"]=> string(10) "Porche 911" ["slug"]=> string(10) "porche-911" ["term_group"]=> int(0) ["term_taxonomy_id"]=> int(26) ["taxonomy"]=> string(8) "post_tag" ["description"]=> string(0) "" ["parent"]=> int(0) ["count"]=> int(0) ["filter"]=> string(3) "raw" } } array(3) { [0]=> object(WP_Term)#7503 (10) { ["term_id"]=> int(25) ["name"]=> string(6) "Jaguar" ["slug"]=> string(6) "jaguar" ["term_group"]=> int(0) ["term_taxonomy_id"]=> int(25) ["taxonomy"]=> string(8) "post_tag" ["description"]=> string(0) "" ["parent"]=> int(0) ["count"]=> int(0) ["filter"]=> string(3) "raw" } [1]=> object(WP_Term)#7490 (10) { ["term_id"]=> int(24) ["name"]=> string(13) "Mercedes K100" ["slug"]=> string(13) "mercedes-k100" ["term_group"]=> int(0) ["term_taxonomy_id"]=> int(24) ["taxonomy"]=> string(8) "post_tag" ["description"]=> string(0) "" ["parent"]=> int(0) ["count"]=> int(0) ["filter"]=> string(3) "raw" } [2]=> object(WP_Term)#7489 (10) { ["term_id"]=> int(26) ["name"]=> string(10) "Porche 911" ["slug"]=> string(10) "porche-911" ["term_group"]=> int(0) ["term_taxonomy_id"]=> int(26) ["taxonomy"]=> string(8) "post_tag" ["description"]=> string(0) "" ["parent"]=> int(0) ["count"]=> int(0) ["filter"]=> string(3) "raw" } }
So the name of my taxonomy field is called 'make' and for test purposes I just want to display all the posts that have a Taxonomy of 'Jaguar'
Please advise.
I managed to sort this in the end, #mrben522 was quite right that the args should be as follows:-
$args = array(
'posts_per_page'=> -1,
'post_type' => 'episode',
'tax_query' => array(
array(
'taxonomy' => 'post_tag',
'terms' => array('Jaguar'),
'field' => 'name',
)
),
'order' => 'DESC',
);
However, this alone didn't resolve the issue. What I also had to do is change the option for the Taxonomy field within ACF. The 'Save Terms' option was set to 'No' but changing this option to 'Yes' got this working for me.
If you are using the 'Save Terms' setting in the taxonomy field, the selected terms will be saved as connections between the post and the term - just like WP core. This means that all the code will work with or without ACF.
Posting this solution as it may be helpful to someone else, thanks!
array(
'taxonomy' => 'name',
'field' => 'make',
'terms' => array('Jaguar')
)
is wrong. should be
array(
'taxonomy' => 'post_tag',
'field' => 'name',
'terms' => array('Jaguar')
)
Check out the docs on WP Query taxonomy parameters for info on how that works
EDIT: looking at the var dump you posted is appears that Jaguar is a post tag, not a custom taxonomy called 'make'
Im editing a plugin because I want to create a checkbox for the tags the plugin has. In this moment Ive got in a variable, this array:
array(9) { [129]=> object(EM_Tag)#84 (15) { ["id"]=> string(3) "129" ["term_id"]=> string(3) "129" ["name"]=> string(35) "Accessible for non-English speakers" ["slug"]=> string(11) "non-english" ["term_group"]=> string(1) "0" ["term_taxonomy_id"]=> string(3) "129" ["taxonomy"]=> string(10) "event-tags" ["description"]=> string(0) "" ["parent"]=> string(1) "0" ["count"]=> string(1) "0" ["fields"]=> array(0) { } ["required_fields"]=> array(0) { } ["feedback_message"]=> string(0) "" ["errors"]=> array(0) { } ["mime_types"]=> array(3) { [1]=> string(3) "gif" [2]=> string(3) "jpg" [3]=> string(3) "png" } } }
There are more tags but I just put one. I would like to generate a checkbox for each tag.
One solution is to iterate over the array that you provided and access the fields that way. I made a shortened array with proper indentation based on your example provided. It seems to be the same but let me know otherwise.
$array = array(
129 => array(
'id' => '129',
'name' => 'Accessible for non-English Speakers'
),
130 => array(
'id' => '130',
'name' => 'A second piece of information'
),
131 => array(
'id' => '131',
'name' => 'A third piece of information'
)
);
// Iterate over the array
foreach ($array as $c) {
// Access the required data
$id = $c['id'];
$name = $c['name'];
// Generate your checkbox
print "<input type='checkbox' name='$name' id='$id'>";
}
I have the following Models with the following relations between them (I just posted the relevant information about them).
Persona.php
public $hasMany = array(
'PersonaHasLdaphost' => array(
'className' => 'PersonaHasLdaphost',
'foreignKey' => 'persona_id'
)
);
Ldaphost.php
public $hasMany = array(
'PersonaHasLdaphost' => array(
'className' => 'PersonaHasLdaphost',
'foreignKey' => '__ldaphosts_id',
'dependent' => false
)
);
PersonaHasLdaphost.php
public $belongsTo = array(
'Persona' => array(
'className' => 'Persona',
'foreignKey' => 'persona_id',
),
'Ldaphost' => array(
'className' => 'Ldaphost',
'foreignKey' => '__ldaphosts_id',
)
);
I have other models, even Persona itself, with this kind of relation working just fine.
But with those ones, when I query the database with a find:
$this->Persona->Behaviors->load('Containable');
$options = array('conditions' => array('Persona.' . $this->Persona->primaryKey => $id),
'contain' => array(
'Personaacceso',
'Personainterna',
'PersonaHasLdaphost' => array('Ldaphost')),
'recursive'=>1);
$persona = $this->Persona->find('first', $options);
I get this weird ouput:
["PersonaHasLdaphost"]=> array(2) {
[0]=> array(4) {
["id"]=> string(3) "154"
["persona_id"]=> string(3) "315"
["Ldaphost"]=> array(0) {}
["PersonaHasLdaphost"]=> array(1) {
[0]=> array(1) {
["__ldaphosts_id"]=> string(2) "41"
}
}
}
[1]=> array(4) {
["id"]=> string(3) "174"
["persona_id"]=> string(3) "315"
["Ldaphost"]=> array(0) {}
["PersonaHasLdaphost"]=> array(1) {
[0]=> array(1) {
["__ldaphosts_id"]=> string(3) "120"
}
}
}
}
When it should be something like:
["PersonaHasLdaphost"]=> array(2) {
[0]=> array(4) {
["id"]=> string(3) "154"
["persona_id"]=> string(3) "315"
["__ldaphosts_id"]=> string(2) "41"
["Ldaphost"]=> array(0) {}
}
...
With data inside "Ldaphost" of course, cause there is an entry in the ldaphost table for those ids.
So can anyone give me a hint why this is happening? I can't see why this one is throwing different results than the others.
I finally managed to solve it.
Seems cakePHP doesn't like dealing with database tables and/or column names starting with double underscore: __ldaphosts table and __ldaphosts_id.
Once I tried with both names modified, my data was returned correctly:
["PersonaHasLdaphost"]=> array(1) {
[0]=> array(4) {
["id"]=> string(3) "165"
["persona_id"]=> string(3) "455"
["ldaphosts_id"]=> string(3) "120"
["Ldaphost"]=> array(8) {
["id"]=> string(3) "120"
["hostname"]=> string(7) "xxxx"
["ip"]=> string(14) "xxx.xx.xxx.xxx"
["mac"]=> string(17) "xx:xx:xx:xx:xx:xx"
["tipo"]=> string(6) "server"
["encendible"]=> bool(false)
["apagable"]=> bool(false)
["descripcion"]=> string(24) "xxxxxxxxxxxxxxxxxxxxxxx"
}
}
}
I'm trying to create a multidimensional array from a form post. This is the dump from that post:
array(8) {
["check"]=>
int(1)
["option_page"]=>
string(19) "content_boxes_group"
["action"]=>
string(6) "update"
["_wpnonce"]=>
string(10) "0adb157142"
["_wp_http_referer"]=>
string(39) "/wp-admin/themes.php?page=home-settings"
["title"]=>
array(3) {
[1]=>
string(9) "Downloads"
[2]=>
string(7) "Columns"
[3]=>
string(4) "Apps"
}
["id"]=>
array(3) {
[1]=>
string(21) "k2-settings-downloads"
[2]=>
string(19) "k2-settings-columns"
[3]=>
string(16) "k2-settings-apps"
}
["order"]=>
array(3) {
[1]=>
string(1) "1"
[2]=>
string(1) "2"
[3]=>
string(1) "3"
}
}
I'm trying to make it look like this:
array(
array('title' => 'Downloads', 'id' => 'k2-settings-downloads', 'order' => '1'),
array('title' => 'Columns', 'id' => 'k2-settings-columns', 'order' => '2'),
array('title' => 'Apps', 'id' => 'k2-settings-apps', 'order' => '3')
);
How can I do this?
something like this?
$post = $_POST['your_array'];
$output = array();
$titles = $post['title'];
$ids = $post['id'];
$orders = $post['order'];
foreach($titles as $id => $title){
$output[] = array("title"=>$title,"id"=>$ids[$id],'order'=>$orders[$id]);
}