1

I am trying to execute the following code:

$today123 = date('Ymd');
$args123 = array (
        'post_type' => 'myType',
        'meta_query' => array(
            'relation'=>'AND',
            array(
                'key'       => 'date',//this is a custom field, not the std 'date' field of wp
                'compare'   => '>=',
                'value'     => $today123,
            )
        ),
        'posts_per_page' => 10,
        'orderby' => 'meta_value_num',
        'order' => 'ASC' 
);
$variable=new WP_Query($args123);

to filter my custom post type based on a custom field, i use the query in a loop as intended and i check out the output with a couple of var_export

the query is completly ignoring my meta_query (i tried deleting the orderby but even without it, the metaquery simply isn't beign applied)

so i tempered a little with the code and checkout various outcomes in various situation and i found out something,

if use

'compare'   => '>=',

it gets ignored, and it returns all the posts even the ones that doesn't have a

'meta_date' >= date('Ymd')

but if i use the exact same code, putting

'compare'   => '<=',

magically the filter gets applied and working as i would expect the <= compare to work.

I may be missing something here, i need to retrieve the 'myType' posts that have a value of meta_date greater or equal to date('Ymd') and sort them by the meta_data in a ASC order...

Why compare '<=' works and compare '>=' doesn't? How can i achieve my desidered result?

EDIT:

"Funny" discovery: if i don't orderby and order, compare >= works...

  • what do you get if you var_dump($variable) – Nicola Peluchetti Apr 22 '18 at 22:59
  • the wordpress query, with all the meta_query and everything else, <= and >= print the exact same query, only difference is the compare itself. This leads me to believe that the error is in wordpress itself because the WP_query has no errors... – Yuri Scarbaci Apr 22 '18 at 23:05
  • Maybe you should just set the orderby to meta_value and not meta_value_num? – Sally CJ Apr 23 '18 at 3:15
  • Sally CJ i tried that, i also tried with meta_value_date but all the same, it simply gets ignored only when using > and >= operators ( <= and = operators works without any problem) – Yuri Scarbaci Apr 23 '18 at 14:38
  • 1
    Thanks for your insight, i figured out a solution by myself after experimenting some more with the code, i will post it a solution now – Yuri Scarbaci Apr 24 '18 at 16:33
2

After a full investigation and a lot more debugging and code tempering I found out that using "date" as a meta field name generate a lot of "confusion" inside the wordrpess query itself as wordpress confuse the meta_query "date" field with the "date" itself, to avoid all this the solution is simply changing the name of the metafield from date to meta_date, doing that makes all the query on the field work as intended!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.