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.
Related
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.
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.
OK you would think this is a fairly trivial piece of code, but it is not working.
The code below is not broken, it is just not working as it should.
Ok, in wordpress I have a custom field in my custom post-type: individual, this custom field has a meta_key of: login_email
The contents of this meta_key is simply email addresses.
Here are some examples of the contents..
bob.dougal#gmail.com, bob.dougal#company.com, bob.dougal#hotmail.com
or..
bob.dougal#gmail.com, bob.dougal#hotmail.com
or even just a single email string..
bob.dougal#hotmail.com
OK, so now you seen the string contents of the custom field. I can explain what I am trying to achieve.
I have this variable... $current_user_email = $current_user->user_login;
Which is a single email address string.
I then need to find if this email address exists within the meta_key contents. So this how I've done it...
$lastposts = get_posts(array(
'posts_per_page' => 1,
'post_type' => 'individual',
'post_status' => 'private',
'meta_query' => array(
array(
'key' => 'login_email',
'value' => $current_user_email,
'compare' => 'IN'
)
)
));
In my example above, if I echo $current_user_email it outputs bob.dougal#company.com
But even though bob.dougal#company.com exists in one of the custom fields like this... bob.dougal#gmail.com, bob.dougal#company.com, bob.dougal#hotmail.com, my get_post returns nothing.
If I then go to the post editor and remove all the other emails from the custom field so bob.dougal#company.com is the only text in the custom field, then the above query works!
My question is, how can I get meta_query to find bob.dougal#company.com within a meta_key which contains this string: bob.dougal#gmail.com, bob.dougal#company.com, bob.dougal#hotmail.com
Because 'IN' is not doing what it is meant to.
Code taken from http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters
Thanks
I believe this may be happening because you are storing multiple email addresses in one key, as opposed to having multiple keys with one email address each.
Because of this, IN will not work as it checks if a given value exists within a set of possibilities (similar to how the PHP in_array() works).
What you need to do is replace IN with LIKE. This will check the whole string for occurrences of your email address and return matches.
You should be careful though, as this can lead to inconstant results. For example, if $current_user_email = bob.dougal#company.com; then all of the following will be true -
bob.dougal#gmail.com, 1bob.dougal#company.com, bob.dougal#hotmail.com
bob.dougal#gmail.com, 12bob.dougal#company.com, bob.dougal#hotmail.com
bob.dougal#gmail.com, 123bob.dougal#company.com, bob.dougal#hotmail.com
I'll grant you that it is unlikely, but that's why you should not use strings to store multiple values. I'd strongly suggest using multiple keys (you can have many with the same name) and then you can use IN in your query. This would also guarentee only exact matches.
$current_user_email is a string. So transform this string into an array:
$current_user_email = explode(", ", $current_user_email);
$lastposts = get_posts(array(
'posts_per_page' => 1,
'post_type' => 'individual',
'post_status' => 'private',
'meta_query' => array(
array(
'key' => 'login_email',
'value' => $current_user_email,
'compare' => 'IN'
)
)
));
This might seem a little odd as a method.
I have posts on my wordpress website, which has a custom field called 'login_email'
In my theme files, I have this query...
$press = new WP_Query(array(
'posts_per_page' => -1,
'post_type' => 'download',
'post_status' => 'private',
'meta_query' => array(
array(
'key' => 'login_email',
'value' => $current_user->user_login,
'compare' => '='
)
)
));
This simply retrieves private posts which has the login_email custom field value which that matches $current_user->user_login
Currently in my login_email custom field i have this value...
josh#samplemail.uk.com
No what I would like to be able to do is add more emails to this custom field seperate by a comma like this...
josh#samplemail.uk.com, colin#samplemail.uk.com, jimbo#samplemail.uk.com
...and for my query above to be able to recognise one email out of this list.
For example if the user colin#samplemail.uk.com is logged in, then because colins email exists in this custom field (along with josh & jimbo's email) then the post will be returned in the query.
I hope this makes some kind of sense. Let me know if I need to elaborate more.
It's like I need to modify the compare in the query - but these are the only options I can find...
compare (string) - Operator to test. Possible values are '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN', 'EXISTS' (only in WP >= 3.5), and 'NOT EXISTS' (also only in WP >= 3.5). Default value is '='. Source
...I've tested EXISTS but does not return any posts.
Has any one got a clever fix or idea?
Thanks
Josh
Please try the operator IN. This will allow you to search in multiple emails.
Follow the IN clause written here: http://core.trac.wordpress.org/ticket/14997
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'.