Reputation: 311
I am using Wordpress Advanced Custom Fields and want to search posts based on those fields and user input. I have four fields: start (date), end (date), stage, and type. The user can choose any combination of these fields to search on. I automatically give them a start and end date if they do not choose one, for the current year to date. Stage and Type are otherwise empty. I am having a problem adding the meta_queries to the overall get_posts query.
I have tried using array_push() as well.
The Date query works and the Stage query works until I try to add the Type.
The current code should return two results but is only returning one.
$start = trim($_POST['start']);
// sets default date range to the current year
if (empty($start)) {
$start = 'January 1, ' . date('Y');
}
$end = trim($_POST['end']);
$type = trim($_POST['type']);
$stage = trim($_POST['stage']);
// sets the date range query
$query = array (
'posts_per_page' => 10,
'post_type' => 'quote',
'date_query' => array (
array (
'after' => $start,
'before' => $end,
'inclusive' => true,
)
)
);
$meta_query = array();
if (!empty($stage)) {
$meta_query[] = array(
'key' => 'stage',
'value' => $stage,
'compare' => '='
);
}
if (!empty($type)) {
$meta_query[] = array(
'key' => 'type',
'value' => $type,
'compare' => '='
);
}
if (!empty($meta_query)) {
$meta_query['relation'] = 'AND';
$query['meta_query'] = $meta_query;
}
$posts = get_posts($query);
Upvotes: 1
Views: 20
Reputation: 311
Solved: It turns out the field name "type" is already in use by Wordpress. Duh.
Upvotes: 1