A
A
Alexey Gorbunov2020-03-07 09:19:38
Task Schedulers
Alexey Gorbunov, 2020-03-07 09:19:38

How to pass the necessary record to $post_id for the cron event and updating the update_post_meta fields according to the schedule?

Good afternoon. How to get the post ID and update the update_post_meta entries on a schedule?

// регистрируем интервал 24 часа
add_filter( 'cron_schedules', 'cron_24' );
function cron_24( $schedules ) {
  $n = rand(0,55);
  $schedules['rand_24'] = array(
    'interval' => 60 * $n * 24,
    'display' => 'раз в сутки'
  );
  return $schedules;
}

// регистрируем событие 24_event
add_action( 'wp', 'to_cron_24' );
function to_cron_24() {
  if ( ! wp_next_scheduled( '24_event' ) ) {
    wp_schedule_event( time(), 'rand_24', '24_event');
  }
}

// добавляем функцию к хуку события
add_action( '24_event', 'updated_meta', 1 );

function updated_meta() {

    // какая-то задача, например ключевые слова
    $keywords = 'ключевые фразы для серии постов';

    update_post_meta( $post_id, 'allkeywords', $keywords );
}


When I enter a specific post id , it works, but when the parameter: $post_id does not work .

function updated_meta() {

    // какая-то задача, например ключевые слова
    $keywords = 'ключевые фразы для серии постов';

    update_post_meta( 34, 'allkeywords', $keywords ); // только так работает

    update_post_meta( $post_id, 'allkeywords', $keywords ); // так нет
    update_post_meta( $post->ID, 'allkeywords', $keywords ); // так нет
    update_post_meta( get_the_ID(), 'allkeywords', $keywords ); // так нет

add_action( '24_event', 'updated_meta', 1 );


$post = get_post(); // tried
global $post; // tried
global $wpdb; // tried

And more. For the usual updating of fields through save_post in the function, we write the function updated_meta ( $post_id ) parameter, but this does not work for the event.

Probably the ID is not defined with empty brackets function updated_meta ( ) , that is, the function does not understand for which specific post (record) to work.


How to get post ID and update custom 'allkeywords' field for all posts at once?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
nokimaro, 2020-03-07
@AGorbunoff

inside function updated_meta() write code to get the necessary $post_id from the database?
Google suggests that this is done through get_posts ()

$args = array(
  'numberposts' => 10,
  //'post_type'   => 'book'
);
 
$latest_posts = get_posts( $args );

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question