1

How can I fix the date with multiple Wordpress CPT date? An example, the custom post "John The Man" on "Canada" CPT the birthday is "Thursday 05 May 1960". How can I compare with only date and month (05 May) with every year (2024, 2025, etc), and skipping birthyear+time? Now it is all records/row without date, or no records/row with date. I have tried 'where' and 'meta_query' on the dates.

//index.php:

echo birthday("Canada", "dmy", "birthday", "Canada ...");
echo birthday("USA", "dmy", "birthday", "USA ...");
echo birthday("Mexico", "dmy", "birthday", "Mexico ...");
echo birthday("El Salvador", "dmy", "birthday", "El Salvador...");​
//and so on ​...

​
function birthday($custompost, $dmy, $birthday, $message) {
    $current_date = new DateTime();
    $datemonth = $current_date->format("d M");
    $args = array(
        'post_type' => $custompost,
        'post_status' => 'publish, private',
        'limit'    => '-1',

//this does not working with 'where'
        //'where' => "DATE_FORMAT(CAST(birthday.meta_value AS date), '%d %c') = '$datemonth'", //not working

//this does also not working with 'meta_query'
        'meta_query' => array(
            array(
            'key' => 'birthday', 

            'value' => $datemonth, //not working
            //'value' => '05 May', //not working
            //'value' => '%5%May%', //not working

//must remove the year+time, or else it can be only 1 birthday (e.g. Thursday 05 May 1960) for each country
            //'value' => date('Y/m/d'),
            //'value' => date('Y/m/d',time()),
​
            'type' => 'DATE', //or 'NUMERIC', not working

            //'compare' => 'LIKE', //or EXISTS, IN, could compare with from "Thursday 05 May 1960" to "05 May", skipping day and year. Not working
            )
        ) //end meta_query
    ); //end args

$posts = get_posts( $args );
echo "<pre>posts:";print_r( $posts );echo "</pre>";
echo "<pre>query:";print_r( $query );echo "</pre>";
}; //end function​
​```

1
  • 1
    maybe create a custom taxonomy terms for day-month for each post and filter the posts by that term? taxonomies are made for filtering and have usually better performance than using meta filtering.
    – birgire
    Commented May 5 at 10:40

1 Answer 1

0

If your posts store the date in a consistent format like YYYY-MM-DD HH:MM:SS, etc, you could compare with strings using regular expressions, like:

$current_date = new DateTime();
$datemonth = $current_date->format("m-d"); // MM-DD

$args = array(
  // …
  'meta_query' => array(
    array(
      'key'     => 'birthday',
      'compare' => 'REGEXP',
      // Regular expression explanation:
      // ^     - Start of string
      // \d    - Any digit (0-9)
      // {4}   - Four of the previous token
      // \d{4} - Four digits
      'value'   => "^\d{4}-$datemonth",
1
  • Solved. Thank you.
    – Oldie
    Commented May 6 at 10:42

Your Answer

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

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