0

I have some custom meta fields. Some are date fields but WordPress saves everything as a string, so how can I compare the dates?

$meta_query_args = array(
    'relation' => '', // Optional, defaults to "AND"
    array(
        'key'     => 'my_custom_date_field',
        'value'   => '',
        'compare' => '>'
    )
);
$meta_query = new WP_Meta_Query( $meta_query_args );

I found this, but how can I add some more conditions with dynamic variable / prepared statement?

5
  • 1
    Do you have it in the preferred format (YYYYMMDD)? WP_Query Codex: "The 'type' DATE works with the 'compare' value BETWEEN only if the date is stored at the format YYYYMMDD and tested with this format." - that's a start.
    – Howdy_McGee
    Oct 16, 2014 at 19:59
  • thanks . i will change it and try . can i use prepared statement with WP_Query ?
    – zod
    Oct 16, 2014 at 20:06
  • 1
    Try: wordpress.stackexchange.com/a/82965/21376
    – s_ha_dum
    Oct 16, 2014 at 20:10
  • @zod Prepared statement such as SQL? Not with WP_Query but you can use $wpdb just make sure you always use prepare() for safety.
    – Howdy_McGee
    Oct 16, 2014 at 20:10
  • 1
    Or: wordpress.stackexchange.com/a/124163/21376
    – s_ha_dum
    Oct 16, 2014 at 20:10

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.