admin管理员组文章数量:1168540
I'm trying to run a query that only shows posts if it's meta value has a date string that is LESS than the current date's string. An example is below;
<?php
$current = date('Ymd'); // Eg: today would be 20131011
$date = get_post_meta($post->ID, 'event_date', true); // (Eg; 19690816)
$args (
'post_type' => 'event',
'posts_per_page' => -1,
'meta_query' => array(
'key' => 'event_date',
'value' => $current,
'compare' => '<',
'type' => 'numeric'
)
);
?>
But it doesn't seem to be working.
The $date
value is defined by a meta box and returns a string in the same format as the $current
value above so it's basically just comparing 2 numeric values against each other.
Anyone have any ideas? I've tried it the opposite way, using the greater than symbol and that doesn't want to work either.
I've quadruple checked the post type name, the values, etc and all are correct.
I'm trying to run a query that only shows posts if it's meta value has a date string that is LESS than the current date's string. An example is below;
<?php
$current = date('Ymd'); // Eg: today would be 20131011
$date = get_post_meta($post->ID, 'event_date', true); // (Eg; 19690816)
$args (
'post_type' => 'event',
'posts_per_page' => -1,
'meta_query' => array(
'key' => 'event_date',
'value' => $current,
'compare' => '<',
'type' => 'numeric'
)
);
?>
But it doesn't seem to be working.
The $date
value is defined by a meta box and returns a string in the same format as the $current
value above so it's basically just comparing 2 numeric values against each other.
Anyone have any ideas? I've tried it the opposite way, using the greater than symbol and that doesn't want to work either.
I've quadruple checked the post type name, the values, etc and all are correct.
Share Improve this question asked Oct 11, 2013 at 11:09 PoisontonomesPoisontonomes 6064 gold badges17 silver badges28 bronze badges2 Answers
Reset to default 1A meta_query
is an array of arrays. You only have an array (and a syntax error). You need this:
$current = date('Ymd'); // Eg: today would be 20131011
$date = get_post_meta($post->ID, 'event_date', true); // (Eg; 19690816)
$args = array(
'post_type' => 'event',
'posts_per_page' => -1,
'meta_query' => array( // an array
array( // of arrays
'key' => 'event_date',
'value' => $current,
'compare' => '<',
'type' => 'numeric',
)
)
);
$q = new WP_Query($args);
var_dump($q->request);
I doubt that 'type' => 'numeric',
is necessary though.
Notice the last line... very useful for debugging these things.
Your code looks valid for me. The only thing you can try is 'NUMERIC' instead of 'numeric' and to cast $current
to int
as date()
will return a string.
$current = (int)date('Ymd');
To debug this further you can place this directly under your code:
global $wpdb;
var_dump( $wpdb->last_query );
You'll then have the complete query and can paste it into a sql-shell (phpMyAdmin or whatever you prefer) to see what's wrong with it.
If this doesn't solve your question feel free to edit your question with the complete SQL query and I'll have a look at it.
本文标签: wp querymetaquery compare not doing what I want it to do
版权声明:本文标题:wp query - meta_query compare not doing what I want it to do 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1737602522a1998378.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论