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
Related
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
I got this wordpress array and need a while loop inside for every 'AND' relation:
$query_args = array(
'post_type' => 'post',
'post_status' => 'publish',
'meta_query' => array(
'relation' => 'OR',
array(
'relation' => 'AND',
array(
'key' => 'foo_0_start',
'compare' => '>=',
'value' => '$start'
),
array(
'key' => 'foo_0_end',
'compare' => '<=',
'value' => '$end'
)
),
array(
'relation' => 'AND',
array(
'key' => 'foo_1_start',
'compare' => '>=',
'value' => '$start'
),
array(
'key' => 'foo_1_end',
'compare' => '<=',
'value' => '$end'
)
)
)
);
I was searching for hours and tried to build a function without success. How can I accomplish this issue? And what happens with the "'relation' => 'OR',"?
$query_args = array(
'post_type' => 'post',
'post_status' => 'publish',
'meta_query' => array()
);
$i = 1;
while ($i<=5;) :
$i++
$query_args['meta_query'][] = array (
'relation' => 'AND',
array(
'key' => 'foo_$i_start',
'compare' => '>=',
'value' => '$start'
),
array(
'key' => 'foo_$i_end',
'compare' => '<=',
'value' => '$end'
)
);
endwhile;
Help would be highly appreciated.
ok. Found the solution:
$query_args = array(
'post_type' => 'post',
'post_status' => 'publish',
'meta_query' => array('relation' => 'OR')
);
$i = 1;
while ($i<=5) :
$i++;
$query_args['meta_query'][] = array (
'relation' => 'AND',
array(
'key' => 'foo_' . $i . '_start',
'compare' => '>=',
'value' => '$start'
),
array(
'key' => 'foo_' . $i . '_end',
'compare' => '<=',
'value' => '$end'
)
);
endwhile;
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..
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"
$this->paginate['Member'] = array(
'conditions' => array($conditions,'Member.division_id' => $currentTeam['Division']['id'],'Member.team_id'=> array(null,0)),
'group' => 'Member.id',
);
This query does not get the NULL team id.. It gets just the team id 0.
Using the IS NULL keyword usually works.
array(
$conditions,
'Member.division_id' => $currentTeam['Division']['id'],
'OR' => array(
'Member.team_id' => 0,
'Member.team_id IS NULL'
)
)
Can you try this?
$this->paginate['Member'] = array(
'conditions' => array($conditions,
'Member.division_id' => $currentTeam['Division']['id'],
'OR'=> array(
array('team_id'=>null),
array('team_id'=>0),
),
'group' => 'Member.id',
);
Edit: Wrapped each team_id condition in it's own array. This prevents the issue of a duplicated 'team_id' array key, while still using a consistent format for the conditions.
array(
$conditions,
'Member.division_id' => $currentTeam['Division']['id'],
'OR' => array(
'Member.team_id' => 0,
'IS' => array( 'Member.team_id' => NULL )
)
)
This is what you want.
$this->paginate['Member'] = array(
'conditions' => array($conditions,
'Member.division_id' => $currentTeam['Division']['id'],
'OR'=> array('Member.team_id'=> null, 'Member.team_id'=> 0),
'group' => 'Member.id',
);