M
M
Maxim Tsyplenkov2021-04-14 23:22:02
Task Schedulers
Maxim Tsyplenkov, 2021-04-14 23:22:02

How to make daily change of publication date of 2 posts in wordpress using php and cron?

PHP version: 7.4.13 (alt)
Database
server Server: MySQL (Localhost via UNIX socket)
Server type: MySQL Server
version: 5.6.50 - MySQL Community Server (GPL)
Protocol version: 10

There are two pages in wordpress whose publication date is requires daily updates.

Page data in database:

Page 1:
Table: wp_posts
ID: 719
post_date: 2021-04-14 20:55:17
post_date_gmt: 2021-04-14 17:55:17
post_modified: 2021-04-14 20:55: 54
post_modified_gmt: 2021-04-14 17:55:54

Page 2:
Table: wp_posts
ID: 50913
post_date: 2021-04-14 20:55:17
post_date_gmt: 2021-04-14 17:55:17
post_modified: 2021-04-14 20:55:54
post_modified_gmt: 2021-04-14 17:55:54 Tell me

how can I use php and cron to update the date daily to the current one (leave the time as it is)?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
D
Denis Yuriev, 2021-04-15
@dyuriev

In my humble opinion, if you look closely at the question, you can see the answer
Write a php script that will update the date to the current one using an sql query
. The script itself will be 10 lines at the most.
If you can still change the time, then you can update it with one compound request.
PS: what is your goal?

D
Daria Motorina, 2021-04-15
@glaphire

You can write this script regardless of wordpress features. There is enough bare php and bare sql queries through pdo. These are two update requests. Adding a script to crontab is also one line to add (there are a lot of examples, there was even an interactive site that described on the fly the decoding of the cron run frequency)

R
Rsa97, 2021-04-15
@Rsa97

You don't even need PHP here. It is enough to write one query using NOW() and WHERE ... IN and execute it through the mysql console at a given time.

A
Artem Zolin, 2021-04-15
@artzolin

I agree with my colleagues - there is enough bare php and bare sql, but if you desperately need WordPress functions, then here is an example. You need to make a function out of it and hang it on cron using the wp_schedule_event () function

date_default_timezone_set("Europe/Moscow");

$posts = get_posts( array(
  'numberposts' => -1,
  'post_type'   => 'event',
) );

foreach ( $posts as $key => $post ) {
  $post_data = array(
    'ID' => $post->ID, 
    'post_date' => date('Y-m-d H:i:s'),
    'post_date_gmt' => gmdate('Y-m-d H:i:s'),
    'post_modified' => date('Y-m-d H:i:s'),
    'post_modified_gmt' => gmdate('Y-m-d H:i:s'),
  );

  // обновляем запись в базе данных
  wp_update_post( wp_slash( $post_data ) );
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question