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

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.

Related

Create a variable from a tax query in Wordpress

I am using the acf load_value function to select a default value for a field. I need to run a tax query and store it in a variable to use as my default value. Here is the load_value code:
function my_acf_load_field_five( $field ) {
$field['default_value'] = $VARIABLE_HERE;
$field['disabled'] = 1;
return $field;
}
The variable ($VARIABLE_HERE) needs to a taxonomy id which I need to grab from a tax query. The tax query I need is this:
private function get_latest_news() {
$args = [
'post_type' => 'news',
'tax_query' => array(
array(
'taxonomy' => 'issue',
'field' => 'slug',
'terms' => get_field('issues_controller')->slug,
)),
'posts_per_page' => 3,
]
$results = new WP_Query( $args );
}
The following query does get the result I want. The problem I'm having is how do I then store the result of that query - which should be a simple tax id such as 233 - into a variable I can pass to the load_value function?
It is the linking of the two things I'm struggling with. Although I have used them, I don't have much experience with tax queries, when googling it shows endless examples of them but nothing to show how I would create a variable with the output.

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.

Check if string exists in meta key string using meta_query check in wordpress get_posts function

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'
)
)
));

CakePHP findList doesn't return aggregated values

The following query returns an array containing the proper ids, but null for all values.
If I remove the aggregation function (AVG()), it returns values (not the averaged ones of course), if I choose e.g. find('all') it returns the average, but not in the list format I want (I could work with that, but I want to try to do it with 'list' first).
$progress = $this->Trial->find('list', array(
'fields' => array(
'Trial.session_id',
'AVG(Trial.first_reaction_time_since_probe_shown) AS average_reaction_time'
),
'group' => 'Trial.session_id',
'conditions' => array(
'Trial.first_valid_response = Trial.probe_on_top',
'TrainingSession.user_id IS NOT NULL'
),
'contain' => array(
'TrainingSession' => array(
'conditions' => array(
'TrainingSession.user_id' => $this->Auth->user('id')
)
)
),
'recursive' => 1,
));
The generated SQL query returns exactly the result I want, when I send it to the DB via PhpMyAdmin.
SELECT
`Trial`.`session_id`,
AVG(`Trial`.`first_reaction_time_since_probe_shown`) AS average_reaction_time
FROM
`zwang`.`trials` AS `Trial`
LEFT JOIN
`zwang`.`training_sessions` AS `TrainingSession` ON (
`Trial`.`session_id` = `TrainingSession`.`id` AND
`TrainingSession`.`user_id` = 1
)
WHERE
`Trial`.`first_valid_response` = `Trial`.`probe_on_top`
GROUP BY
`Trial`.`session_id`
I've examined the source for find('list'). I think it's due to the "array path" for accessing the list getting screwed up when using functions in the query, but I couldn't fix it yet (or recognise my abuse of CakePHP logic).
Once I posted the question, Stackoverflow started relating the correct answers to me.
Apparently, it can't be done with 'list' without virtualFields.
I didn't expect that because it worked using the other find-types.
$this->Trial->virtualFields = array(
'average_reaction_time' => 'AVG(Trial.first_reaction_time_since_probe_shown)'
);
$progress = $this->Trial->find('list', array(
'fields' => array('Trial.session_id','average_reaction_time')
/* etc... */
));

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