0

I have a date value inside a WordPress post's content that I need to be replaced every day with the current day.

The string looks like this: 2017-11-11 00:00

I was thinking of building a simple PHP script that would access the MySql wp_posts and find/replace that string, then run the update SQL query to change the required value.

$pattern = '/201\d{1}-\d{1,2}-\d{1,2}\s00:00/';
$today = date("Y-m-d");
$replacement = "$today '00:00'";
$finalString = preg_replace($pattern, $replacement, $originalString);

The post content doesn't change so the regexp doesn't need to be more elaborated than this and also the hour:mins can remain 00:00

Now the ugly part is that I will need to run a chron job, once per day to run this PHP that updates the SQL database.

Is there a better/safer/easier way to solve this issue?

2
1

You can create a new shortcode with this code

add_shortcode("currentDay", function ($attr, $content, $tag) {

    $date = current_time("Y-m-d");

    return $date;

});

and you just have to put [currentDay] in the content

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.