Woocommerce get category loop, exclude another category - loops

I don't know if I'm explaining this correctly, but I have a loop that's pulling all products assigned to a specific category (in this case, Current Season - this is for a performing arts organisation).
But each product is actually assigned to multiple categories for various reasons, and in this case I want all products assigned to 'current-season', but NOT also assigned to the category 'series'.
I've tried the following, but it has done nothing to change my query display.
It's still showing everything assigned to 'current-season'.
$args_right = array(
'post_type' => 'product',
'posts_per_page' => 999,
//'product_cat' => 'current-season',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'series',
'operator' => 'NOT IN'
),
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'current-season',
'operator' => 'IN'
)
),
'meta_key' => 'date',
'orderby' => 'meta_value_num',
'order' => 'ASC'
);
I'm sure I'm just missing something very obvious. I bang my head against the wall, and then wind up going "D'UH"!!
Thanks in advance!

i beleive your problem with the 'meta_key' => 'date' if you want to order the posts by date you can use the following query.
$args_right = array(
'post_type' => 'product',
'posts_per_page' => 12,
'orderby' => 'date',
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'terms' => array('current-season'),
'field' => 'slug',
'operator' => 'IN' // Possible values are 'IN', 'NOT IN', 'AND'.
), array(
'taxonomy' => 'product_cat',
'terms' => array('series'),
'field' => 'slug',
'operator' => 'NOT IN' // Possible values are 'IN', 'NOT IN', 'AND'.
)
),
);
$loop = new WP_Query($args_right);
if ($loop->have_posts()) {
while ($loop->have_posts()) :
$loop->the_post();
wc_get_template_part('content', 'product');
endwhile;
} else {
echo __('No products found');
}
wp_reset_postdata();
Reference
I tested this query locally and it's displaying all product with current-season category and if one product in this category assigned to another category or sub-category series it's excluded

Related

WP_Query - foreach array

In a WP_Query, i need some arrays to be dynamic, when a checkbox-group is checked.
The code so far is:
$args = array(
'post_type' => 'hold',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'hold_serie',
'value' => 'serie3',
'compare' => 'LIKE'
),
array(
'relation' => 'OR',
'options' => array()
),
),
);
$days = array('man', 'tir', 'ons');
foreach ($days as $value_day) {
$args['meta_query']['options'] = array(
'key' => 'hold_day',
'value' => $value_day,
'compare' => 'LIKE'
);
}
I have a checkbox group, which will make the same array as the $days-array().
What I'm hoping to achieve is, that the array with 'key' => 'hold_day' will be repeated three times, and the WP_Query will look at all the posts, that have either "man", "tir" OR "ons".
At this point, the WP_query only takes the post, with the last value in the array, in this case "ons".
If I delete "ons", so the array only have "man" and "tir", it only finds the posts, where "hold_day" = "tir".
What am I doing wrong?
You dose not need to use second part of the code.
You can use an array as the value of meta key, but in that case you need to use "IN" in compare field.
Your code should be
$args = array(
'post_type' => 'hold',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'hold_serie',
'value' => 'serie3',
'compare' => 'LIKE'
),
array(
'relation' => 'OR',
'key' => 'hold_day',
'value' => array('man', 'tir', 'ons'),
'compare' => 'IN'
),
),
);
Try the above code and let me know the result.
Thanks

Count posts based on more than one meta key value

I'm trying to count the number of posts where certain meta key values exist, in this case posts that have a key 'texture' with the value 'soft', and also have a key 'colours' that are 'red', 'blue' or 'green'.
My code does not error, but it appears to count all posts instead of just the ones with the meta key values as mentioned:
$args = array(
'posts_per_page' => -1,
'post_type' => 'post',
'post_status' => 'publish',
'author' => $current_user_id,
'meta_query' => array(
'key' => 'texture',
'value' => 'soft'
),
array(
'key' => 'colours',
'value' => array('red', 'blue', 'green')
)
);
$posts_query = new WP_Query($args);
$the_count = $posts_query->post_count;
echo $the_count;
What am I doing wrong?
Cheers.
Try adding 'relationship' => 'AND' in meta_query and 'compare' => 'IN' in meta array that have multiple values ('red', 'blue', 'green') like this :
'meta_query' => array(
'relationship' => 'AND',
array(
'key' => 'texture',
'value' => 'soft'
),
array(
'key' => 'colours',
'value' => array('red', 'blue', 'green'),
'compare' => 'IN'
)
)
Edit #1:
Maybe adding % mark around your values would solve problem, I'm not 100% sure it would work but you can try it, like this:
'meta_query' => array(
'relationship' => 'AND',
array(
'key' => 'texture',
'value' => 'soft'
),
array(
'key' => 'colours',
'value' => array('%red%', '%blue%', '%green%'),
'compare' => 'IN'
)
)
I hope it works, Thanks..

cant get required records from habtm in cakephp

In Cakephp I need to get the tutors who teach a subject 'primary English'. Instead I get all the tutors with any subject so the condition gets ignored with no error. There is a habtm relationship between tutors and subjects they teach.
$this->Tutor->Behaviors->load('Containable');
$tutors=$this->Tutor->find('all',array(
'contain' => array('Subject',array( 'conditions'=> array('Subject.name' => 'Primary English'))),
'contain' => array('Subject'),
'recursive' =>-1,
// 'order'=> $orderoptions,
'fields'=>array('Tutor.last_name', 'Tutor.first_name','Tutor.id' ),
));
debug( $tutors);
array(
(int) 0 => array(
'Tutor' => array(
'last_name' => 'Wyers',
'first_name' => 'Adele',
'id' => '13'
),
'Subject' => array()
),
(int) 1 => array(
'Tutor' => array(
'last_name' => 'Payet',
'first_name' => 'Allison',
'id' => '7'
),
'Subject' => array(
(int) 0 => array(
'id' => '4',
'name' => 'English - Year 11',
'TutorsSubject' => array(
'id' => '30',
'tutor_id' => '7',
'subject_id' => '4'
)
),
Remove 'recursive' => -1. This prevents it from selecting relationships recursively. If this still doesn't work then put 'recursive' => 2 in.

Containable to do show deeper data or join table

I have 3 tables: projects, project_reminder_users, project_types.
The relations is as follow:
Project => belong to => ProjectType
hasMany => ProjectReminderUser
ProjectReminderUser => belong to => Project
ProjectType => hasMany => Project
I get all data based on who assigned(ProjectReminderUser) by this
$this->Project->ProjectReminderUser->Behaviors->load('Containable');
$this->paginate = array(
'ProjectReminderUser' => array(
'limit' => $limit,
'contain' => array(
'Project' => array(
'ProjectComment',
'ProjectFile',
),
'User'
),
'conditions' => array(
'User.group_id' => $this->Session->read('Auth.User.group_id'),
'ProjectReminderUser.user_id' => $this->Session->read('Auth.User.id'),
'Project.project_status_id' => PROJECT_STATUS_OPEN,
),
'order' => 'Project.due_date',
)
);
$this->set('myTasks', $this->paginate('ProjectReminderUser'));
and the result look like this
array(
'ProjectReminderUser' => array(
'id' => '96',
'user_id' => '1',
'project_id' => '46'
),
'Project' => array(
'id' => '46',
'project_type_id' => '9',
'contact_id' => null,
'company_id' => null,
'subject' => 'Test Modified Field',
'description' => 'Test Modified Field',
'ProjectFile' => array(
(int) 0 => array(
'id' => '19',
'project_id' => '46',
'user_id' => '6',
'file_path' => '46_bhbinary_xmm_1728.jpg',
'notes' => null,
'created' => '2013-11-26 18:37:49'
),
),
'ProjectComment' => array(
)
),
'User' => array(
'password' => '*****',
'id' => '1',
'group_id' => '1',
'email' => 'xxx#xxxxx.com',
'first_name' => 'xxxx',
'deleted' => false,
'displayName' => 'xxxxx'
)
)
In the result There is data for Project.project_type_id, but I would like to have more detail of that. So I can show it as name instead of number. maybe like ProjectType.name instead.
How can I achieve this, so I can sort it in the view? something like this
$this->Paginator->sort('ProjectType.name', 'Type');
The problem is that Paginator doesn't play nice with deep model associations. I think though if rather than using Cake's methods for doing model associations, you instead do your joins manually, you may be able to work out the sorting as you want. See the below discussion.
http://sinkpoint.railsplayground.net/cakephp-pagination-deep-sort-and-habtm/
Worst comes to worse, you may have to also rewrite the model's paginate function to deal with sorting how you need it to.
How about
'contain' => array(
'Project' => array(
'ProjectComment',
'ProjectFile',
'ProjectType',
),
'User'
),
Looks like you did not contain "ProjectType"

saveAll: save new data for related model or use existing record

I'm trying to update data about many shipments with one form.
Shipment model belongsTo Shippingaddress model and Shippingaddress hasMany Shipment.
In interface user can use his previously saved Shippingaddress from select field or fill small form and add new one.
Currently example structure of data which are posted to controller looks like this:
array(
(int) 0 => array(
'Shipment' => array(
'id' => '223',
'shippingaddress_id' => '8',
)
),
(int) 1 => array(
'Shipment' => array(
'id' => '224',
'shippingaddress_id' => '',
),
'Shippingaddress' => array(
'recipient' => 'some name',
'address' => 'some addres data',
'zip' => '00-555',
'city' => 'some city',
)
),
(int) 2 => array(
'Shipment' => array(
'id' => '225',
'shippingaddress_id' => '12',
)
)
)
It would be nice to save Shiippingaddress data and update all Shipments in one transaction with
$this->Shipment->saveAll($data).
Is it possible?
I was trying many combination of posted array structure but without success.
Your $data structure should looks like this:
$data = array(
'Shippingadress' => array(
'0' => array(
'id' => '8',
'Shipment' => array(
'id' => '223'
)
),
'1' => array(
'recipient' => 'some name',
'address' => 'some addres data',
'zip' => '00-555',
'city' => 'some city'
'Shipment' => array(
'id' => '224'
)
),
'2' => array(
'id' => '12',
'Shipment' => array(
'id' => '255'
)
),
)
);
For save use saveMany:
$this->Shippingadress->saveMany($data, array('deep' => true) );
You don't need to specify shippingadress_id to empty string because Cake already takes care of it and put all relevant foreign keys based on the relationship of your models.

Resources