Compare strings as numbers in WP_Query - php

I am performing a WP_Query on a custom post type called "task" that has a field "stage". I need to get all of the posts with a "stage" >= the "stage" of another task. Problem is stage is a string value, so using >= operator in the meta query won't work.
Currently my hack is to create an array that contains string value of numbers $stage to 50, and query for posts who's stage is in that array. This will work for the time being as no stage is above 12, but not a very scalable or dynamic solution. I'm wondering if there is a better way to mutate a value recieved in WP_query before the operator is used on it?
$stage = get_field('stage', $task_id);
$future_tasks = array();
for ($i = intval($stage); $i <= intval($stage) + 50; $i++) {
array_push($future_tasks, strval($i));
}
$tasks_query_args = array(
'post_type' => array('task'),
'posts_per_page' => -1,
'order' => 'DESC',
'meta_query' => array(
'0' => array(
'key' => 'project',
'value' => $project_id,
'compare' => '=',
),
'1' => array(
'key' => 'stage',
'value' => $future_tasks,
'compare' => 'IN',
),
'relation' => 'AND',
),
);
Ideally the second parameter of the query would be something like:
'1' => array(
'key' => 'stage',
'value' => intval($stage),
'compare' => '>=',
),
But that won't work because value of the stage field is a string.
Wondering if there are any ideas about how to better achieve this?

Seems like, using numeric type could solve the problem:
$tasks_query_args = array(
'post_type' => array( 'task' ),
'posts_per_page' => - 1,
'order' => 'DESC',
'meta_query' => array(
'0' => array(
'key' => 'project',
'value' => intval( $project_id ),
'compare' => '=',
'type' => 'numeric',
),
'1' => array(
'key' => 'stage',
'value' => intval( $task_id ),
'compare' => '>=',
'type' => 'numeric',
),
'relation' => 'AND',
),
);

Related

Wordpress | Split queries based on selection

I am implementing the filters for the content searched by users inside my ajax form in frontend, I am working with this query that works perfectly
$args = array(
'post_type' => 'pro',
'posts_per_page' => -1,
'meta_key' => $_POST['categoryfilter'],
'meta_query' => array(
'relation' => 'AND',
array(
'key' => $_POST['categoryfilter'],
'value' => 0,
'compare' => '>=',
'type' => 'NUMERIC',
),
array(
'key' => $_POST['categoryfilter'],
'value' => 100,
'compare' => '<=',
'type' => 'NUMERIC',
),
),
'orderby' => 'meta_value_num',
'order' => $_POST['order'],
);
my goal was to split the query based on the $_POST of the filter on the form
and I wanted to get that when the user selected the filter: firts, I split the above query into this one to get this
'meta_query' => array(
'relation' => 'AND',
array(
'key' => $_POST['categoryfilter'],
'value' => 0,
'compare' => '>=',
'type' => 'NUMERIC',
),
array(
'key' => $_POST['categoryfilter'],
'value' => 100,
'compare' => '<=',
'type' => 'NUMERIC',
),
),
so i tried with this code but it doesn't work:
if( isset($_POST['first'] ))
$args['meta_query'][] =
array(
'relation' => 'AND',
array(
'key' => $_POST['categoryfilter'],
'value' => 0,
'compare' => '>=',
'type' => 'NUMERIC',
),
array(
'key' => $_POST['categoryfilter'],
'value' => 50,
'compare' => '<=',
'type' => 'NUMERIC',
),
);
Where am I doing wrong?
same thing for sorting I would like when the user enters: order, be able to filter by ASC or DESC as in the first query
I think it's just an issue with how you create the meta_query. You are storing it in a sub-array, but it should not. Try to update like this:
if( isset($_POST['first'] ))
$args['meta_query'] =
array(
'relation' => 'AND',
array(
'key' => $_POST['categoryfilter'],
'value' => 0,
'compare' => '>=',
'type' => 'NUMERIC',
),
array(
'key' => $_POST['categoryfilter'],
'value' => 50,
'compare' => '<=',
'type' => 'NUMERIC',
),
);
When doing $args['meta_query'][] = you should push every array of the meta_query one by one, but here you already have the full meta_query.
This assuming you don't have more code that could push more meta_queries rules. If so, then you should push each part at a time.

How to write Wordpress Meta Queries with "if then" sorting logic

Summary
In my Wordpress build, I have a calendar made of custom event posts. These posts have two settings, single day event, and multiple day event. This is set as a boolean. I need to grab these posts, and sort them based on their date.
Problem
The issue I'm facing is that when I query the back-end, I don't know which date to sort by. I've tried using the boolean to create a sort of "if then" logic, but no luck.
Trouble Shooting
What have I tried so far?
This is the query I've been trying to get to work. No luck though
$events_query = new WP_Query (array(
'post_type' => 'events',
'posts_per_page' => '-1',
'meta_query' => array (
'relation' => 'AND',
array (
'key' => 'multiple_day_event',
'value' => '1',
'compare' => '=',
),
array (
'key' => 'start_date',
'value' => date("Ymd"),
'compare' => '>',
),
'relation' => 'OR',
array (
'key' => 'multiple_day_event',
'value' => '0',
'compare' => '=',
),
array (
'key' => 'date',
'value' => date("Ymd"),
'compare' => '>',
)
)
));
Can I use a single date instead of two separate dates?
No, not to my knowledge. Using a single date in place of both has causing other issues in my back-end.
Solution
In short, I need a query that's able to use OR, AND, OR logic. Any ideas?
According to the WP_Meta_Query docs:
Nested arrays can be used to construct complex queries
$meta_query_args = array(
'relation' => 'OR', // Optional, defaults to "AND"
array(
'key' => '_my_custom_key',
'value' => 'Value I am looking for',
'compare' => '='
),
array(
'relation' => 'AND',
array(
'key' => '_my_custom_key_2',
'value' => 'Value I am looking for 2',
'compare' => '='
),
array(
'key' => '_my_custom_key_3',
'value' => 'Value I am looking for 3',
'compare' => '='
)
)
);
$meta_query = new WP_Meta_Query( $meta_query_args );
So your example would look something like this:
$events_query = new WP_Query( array(
'post_type' => 'events',
'posts_per_page' => '-1',
'meta_query' => array (
'relation' => 'AND',
array(
'relation' => 'AND',
array (
'key' => 'multiple_day_event',
'value' => '1',
'compare' => '=',
),
array (
'key' => 'start_date',
'value' => date("Ymd"),
'compare' => '>',
),
),
array(
'relation' => 'OR',
array (
'key' => 'multiple_day_event',
'value' => '0',
'compare' => '=',
),
array (
'key' => 'date',
'value' => date("Ymd"),
'compare' => '>',
)
)
)
) );

Wordpress meta_query OR/AND relations not working

I have a search form with multiple options that are taxonomies. When I select all options, or just one, and click search it never returns any record. I have a problem with understanding relations, and how to properly set arrays.
$args = array('post_type' => array( 'property' ),
'order' => 'DESC',
'orderby' => 'post_date',
'meta_query' => array(
'relation' => 'AND',
array(
array(
'key' => 'property_address_postal_code',
'value' => $search,
'compare' => 'LIKE',
),
array(
'key' => 'property_address_state',
'value' => $search,
'compare' => 'LIKE',
),
array(
'key' => 'property_location',
'value' => $search,
'compare' => 'LIKE',
),
),
array(
'relation' => 'OR',
array(
'relation' => 'AND',
array(
'key' => 'property_price',
'value' => array($_GET['min-price'],$_GET['max-price']),
'type' => 'numeric',
'compare' => 'BETWEEN',
),
),
array(
'relation' => 'AND',
array(
'key' => 'property_price',
'value' => array($_GET['min-price'],$_GET['max-price']),
'type' => 'numeric',
'compare' => 'BETWEEN',
),
),
)
)
);
First off, I'm not sure if you're quite understanding how the relation property applies in this function. It applies AND/OR to other properties on the same level. So your very first relation => AND makes sure your $search variable matches one of the properties you specify AND that the property_price is between the min and max values.
Secondly, with subsequent, nested meta queries, the default relation is AND, so when you try to match $search against different properties, you're actually trying to make sure that $search is matching ALL of the properties you specify. You can fix that by trying to match $search against one property OR another property.
Thirdly, with your comparison of the price against the min and max price, the meta query is malformed, and both of those meta queries are identical. In that case, you don't need a relation since you only have to check against one value.
You can simplify your code as such:
$args = array('post_type' => array( 'property' ),
'order' => 'DESC',
'orderby' => 'post_date',
'meta_query' => array(
'relation' => 'AND',
array(
'relation' => 'OR',
array(
'key' => 'property_address_postal_code',
'value' => $search,
'compare' => 'LIKE',
),
array(
'key' => 'property_address_state',
'value' => $search,
'compare' => 'LIKE',
),
array(
'key' => 'property_location',
'value' => $search,
'compare' => 'LIKE',
),
),
array(
'key' => 'property_price',
'value' => array($_GET['min-price'],$_GET['max-price']),
'type' => 'numeric',
'compare' => 'BETWEEN',
)
)
);
Now, you're making a query that checks if $search is like property_address_potal_code OR like property_address_state OR like property_location AND property_price is between the min and max price.
You might still have to fiddle with it a bit depending on other logic in your code, but I think this should get you going in the right direction.

Date range find in wordpress

In wordpress I have only publish_date(not meta_key) and meta_key "end_date"
Example: event from 2015-06-22(publish_date) to 2015-08-24(meta_ket = "end_date")
now I want search all events
(from 2015-07-05 to 2015-07-24)
or
(from 2015-08-05 to 2015-09-25)
and must show event in example
This code not work:
$opt = array(
'post_type' => 'event_info',
'posts_per_page' => '40',
'paged' => $page_num,
'orderby' => 'post_date ID',
'order' => 'DESC',
'relation' => 'OR',
'meta_query' => array(
'key' => 'end_date',
'value' => array('$date_start','$date_end'),
'compare' => 'BETWEEN',
'type' => 'numeric'),
'date_query' => array(
array(
......
'compare' => 'BETWEEN',
),
),
)
);
relation between meta key and data_query not work,
current idea is create 2 query but I dont want do this
try this : here I added timestamp in startdate and enadate meta key
$chkdt = current_time('timestamp');
$args = array('post_type'=>'event_info', 'post_status' => 'publish','posts_per_page'=>-1,'orderby' => 'ID','order' => 'DESC', 'meta_query' =>
array(
'relation' => 'AND',
/******filter start date and End date****************/
array(
'key' => 'end_date',
'value' => $chkdt,
'compare' => '>=',
'type' => 'NUMERIC',
),
array(
'key' => 'start_date',
'value' => $chkdt,
'compare' => 'NUMERIC',
),
/******END filter start date and End date****************/
),
);

Wordpress: Multiple keys for a query

I'm trying to apply a query, in order to display posts from different custom fields (with the same input - $id)
When testing the code below, I get the whole website to crash...
If I remove the name2 & name3 arrays, it works just fine.
Does anyone have a clue where I got stupid this time?
Thanks!
$example = new WP_Query(array(
'post_type' => 'example',
'paged' => $paged,
'posts_per_page' => '-1',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'name1',
'value' => $id,
'type' => 'numeric',
'compare' => '='
),
array(
'key' => 'name2',
'value' => $id,
'type' => 'numeric',
'compare' => '='
),
array(
'key' => 'name3',
'value' => $id,
'type' => 'numeric',
'compare' => '='
)
)
));

Categories