WordPress get_posts() trouble comparing timestamps in meta_query - php

Good Morning. I am having trouble comparing a timestamp returned as a meta_key (returning as a string) to the current timestamp while running get_posts().
I am trying to get any posts with end_date_time set in the future and I am getting some weird behavior. I am using Advanced Custom Fields and Advanced Custom Fields: Date and Time Picker to set the time.
Here is my code:
$time = time();
$args = array(
'post_type' => 'webinars',
'posts_per_page' => -1,
'meta_key' => 'date',
'orderby' => array('meta_value_num' => 'ASC', 'title' => 'ASC'),
'meta_query' => array(
array(
'key' => 'end_date_time',
'value' => $time,
'compare' => '>='
),
),
);
$webinars = get_posts($args);
The query does not return any results if it is set like this. I know that there is a post with a future timestamp set as removing the meta_query shows it and I can get the saved time stamp. (It is saved as a string).
Is there an issue with comparing strings as numbers in a meta_query? Is there a way to convert end_date_time to an int before doing the comparison?
I have also tried converting time to a string before passing it into the $args, but it does not seem to make a difference.
Has anyone else run into this issue?
Update
I have modified the $time variable to use a past time like this:
$time = time()-43200;
After doing this, the query seems to be working fine. If I set end_date_time to the future it will show in the loop, if I set it to the past it is removed from the loop. It seems that there is some sort of time discrepancy that is causing this. Without the adjusted time, events that happened two hours from now would disappear, but after the adjustment they stay displayed. When they are several hours old they still disappear as needed.
Why would this work with the adjusted timestamp $time = time()-43200;, but not with $time = time();?

If you're using the native ACF's Date Time Picker, you should just add type DATETIME to your meta_query and be sure your value has the required format yyyy-mm-dd hh:mm:ss.
'meta_query' => array(
array(
'key' => 'end_date_time',
'value' => date('Y-m-d H:i:s', $time),
'compare' => '>=',
'type' => 'DATETIME'
),
),
Check Date Time Picker and WP's Meta Queries documentation.

Related

ACF custom date field not matching in wp query

Currently using custom post types as hotel reservations / bookings. The post type includes several custom fields but the one I am struggling with is the date field for check in check out dates.
I am using wp_query to retrieve bookings that match a room name and check in date, I can match them and get bookings by room name but I can never match on the date. I have tried several different date formats and tried many different approaches but nothing seems to be work.
The strange thing is if I retrieve the custom post's just by room name and then perform a match in php like this (if($fullyear === get_field('check_in'))) it returns true so the dates match. Below is my WP query, any help would be much appreciated, I've been going round in circles for days with this one.
$bookingposts = new WP_Query(array(
'numberposts' => 20,
'post_type' => 'bookings',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'check_in',
'type' => 'DATE',
'value' => $fullyear,
'compare' => '='
),
array(
'key' => 'room_number',
'value' => $roomnumber,
'compare' => '=',
),
),
));
I'm working with date format 'd/m/y' and ACF is returning in that format.
Looks like my problem was that when filtering with wordpress dates in custom fields you need to use yyyy-mm-dd format! So once ACF was returning that format everything worked like a charm.

Elgg and the utilization of Relationship_created_time_lower

Finishing of my elgg plugin has come to some issues, after fixing my last question I have encountered another. Apperently I am misusing or misunderstanding the use of the Created time lower and upper functions in Elgg
With the code below:
$monthSpan = (30 * 24 * 60 * 60);
$startTime = time() - $monthSpan;
$MemberDifference = elgg_get_entities_from_relationship(array(
'relationship' => 'member', //get Members
'relationship_guid' => $group->guid, //get individual guid for use
'inverse_relationship' => true,
'type' => 'user', //users are returned
'limit' => 20,
'joins' => array("JOIN {$db_prefix}users_entity u ON e.guid=u.guid"),
'order_by' => 'u.name ASC',
'relationship_created_time_lower' => $startTime, //the furthest back it will reach
'relationship_created_time_upper' => time(), //possibly unneeded, but ensures the closest date is today
'count' => true,
));
Using this function, I built it upon my way to get all of the members in the associated group, theoretically it should now grab any members that registered to that group within the last month. Unfortunately, it instead continues to grab every member in the group, regardless of the time they joined.
Does anyone have any information into where I have went wrong?
Turns out, my version of Elgg was too low, otherwise that entire block of code would work. Working Elgg 1.8, I needed to use the following code:
$MemberDifference = elgg_get_entities_from_relationship_count(array(
'relationship' => 'member',
'relationship_guid' => $Reports->guid,
'inverse_relationship' => true,
'type' => 'user',
'limit' => 20,
'count' => true,
'joins' => array("JOIN {$db_prefix}users_entity u ON e.guid=u.guid"),
'order_by' => 'u.name ASC',
'wheres' => array('r.time_created >=' . $startTime)
));
This works perfectly and brings about exactly what im looking for.

Why won't meta_value recognize the numeric value "1"?

I am currently working a magazine website that consists of multiple issues and various types of articles. I assigned a variable for the custom field issue number, and included it in my query in order to display only posts that are in the issue of the current page. Here's the query.
$args = array(
'post_type' => array('story','letter','interview'),
'meta_query' => array(
array(
'value' => "$issue_number",
),
),
);
$my_query = new WP_Query( $args );
This works for all issues except the first one. For some reason, my query will not recognize the numeric value "1", so I have to literally spell out "one" in the custom field for all articles that belong in the first issue.
Your meta_query is incomplete. You need to specify a key and since it's a numerical value you have to set the type. You'll also want to remove the quotes around the value.
'meta_query' => array(
array(
'key' => '_my_issue_number_key',
'value' => $issue_number,
'type' => 'NUMERIC',
),
),
Keep in mind that the issue may not be in your query but in the way you're saving the value. It would be very easy for the value '1' to be confused with 'true'. If you're still having trouble after making the changes above, open up your postmeta table and check the value being stored.

Podio api task due_date format

I'm trying to get the Podio API (php-library) to return all tasks that have a due date withing a certain range. PodioTask::get_all() works fine, it returns all tasks (limited to 100). Of course that is not very useful so I add attributes as documented:
$tasks = PodioTask::get_all(array(
'org' => [my_organization_id],
'completed' => 'false',
'limit' => 100
)
);
Up until here behavior is still as expected. As soon as I attempt to add the due_date-attribute I get an error message saying Uncaught PodioBadRequestError: "The values are not in the right format" Request URL: http://api.podio.com/task/?org=[my_organization_id]&completed=false&due_date=&limit=100
I try to do it like this, which works fine for the filters on Podio items:
$tasks = PodioTask::get_all(array(
'org' => [my_organization_id],
'completed' => 'false',
'due_date' => array(
'from' => '2d',
'to' => '90d'
),
'limit' => 100
)
);
I tried replacing the from and to dates with absolute ones in the YYYY-MM-DD format (as a string, but also tried a DateTime-object), but I get the same error. I tried removing the array and just setting a single date (as a string and a DateTime object) and none of it works. I keep getting the message that the values are not in the right format. The documentation tells me the due_date-parameter is "The from and to date the task should be due between. For valid options see date filtering under the filter area." If I go there I end up with the documentation for filtering items and I am doing the exact same thing here as for items, which I can filter by date. (https://developers.podio.com/doc/tasks/get-tasks-77949)
It appears like the php-library expects a string, but the API needs the 'from' and 'to' properties.
Anyone got any ideas?
Try this
$attributes = array(
'org' => [my_organization_id],
'completed' => 'false',
'limit' => 100,
'due_date'=> $fromDate.'-'.$toDate
);
$tasks = PodioTask::get_all($attributes);
where $fromDate and $toDate are DateTime Objects.

CakePHP list with SQL functions

I would like generate list like day=>title:
$news = $this->News->find('list', array(
'conditions' => array(
'News.created LIKE' => '2008-09%'),
'fields' => array(
'DAY(News.created) AS day',
'News.name'),
'recursive' => -1));
...but doesn't work, why?
Function SUBSTR/SUBSTRING also...
When I use find with 'all' property, the function DAY works good!
It won't work because DAY(News.created) is a computed value, but it will probably work if you make it a virtual field.
Note that when you use 'all', each row on the results array will have a [0][day] key for that value, instead of [News][day]. This means Cake won't recognize it as a field from model News, and that's why you can't use it with 'list'.

Categories