I want to search SQL database using PHP and look for meta_value. The meta_value contains data like this:


So what I want to do is make it search for the UpdatedDate and see if it is between 2000-01-01 to (todays date - 5 days).

This is what I did so far, which is not working.

global $wpdb; // Global object database variable
$updatedDate = [];

//set how many days odds will show until.. currently set to -5 days.
$endDatefind = (new \DateTime('now'))->modify("-5 day")->format('Y-m-d'); // creating datetime object

//finds all dates from 2000 to 5 days before todays date
$findquery = $wpdb->get_results ( "
  FROM `wp_postmeta`
  WHERE`meta_key` = 'Odd'
  AND STR_TO_DATE(meta_value, '%Y-%m-%d') >= '2000-01-01'
  AND STR_TO_DATE(meta_value, '%Y-%m-%d') =< '$endDatefind'


Any ideas why its not finding any data? I tried to do a dummy data by changing one row with an 'UpdatedDate' of last 2 weeks, but it is not showing. Please help thanks.

  • You can't query data like this if it's stored that way. If you want to query post meta by a date, you need to store that date in its own field. – Jacob Peattie Dec 14 '18 at 14:34

Your Answer

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

Browse other questions tagged or ask your own question.