Обновите все сообщения в пользовательском типе сообщения с помощью wp_cron ()

Я сделал функцию для применения термина таксономии к публикации, если для мета-значения публикации установлено значение true. Это работает как следует.
Проблема, с которой я сталкиваюсь, заключается в том, что она обновляется только после того, как я вручную сохранил / обновил сообщение.
Есть ли способ запланировать это или сделать это динамически для всех сообщений в пользовательском типе сообщения?

Мой код функции термина таксономии: —

function save_cp_term_meta( $post_id, $post, $update ) {
$termshouldbe='new';

$meta_value = get_post_meta( $post->ID, 'new_used_cat', true );
if  (!empty( $meta_value ))
{
$termshouldbe='used';
}
else
{
}

wp_set_object_terms($post_id,$termshouldbe,'vehicle_condition',false);
}
add_action( 'save_post', 'save_cp_term_meta', 10, 3 );

0

Решение

Ты можешь использовать WP_Cron() запланировать выполнение задачи в определенное время каждый день и выполнить обновление.

// check if the next cron is ours, if not schedule it.
if ( ! wp_next_scheduled( 'prefix_save_cp_term_meta_cron' ) ) {
wp_schedule_event( strtotime( '5:15PM' ), 'daily', 'prefix_save_cp_term_meta_cron' );
}
/**
* function to query for posts of a certain post type and update
* some term_meta based on a post_meta value.
*/
function prefix_save_cp_term_meta_runner() {
// make sure to set the correct post type you want here.
$args = array(
'post_type'      => array( 'your post type' ),
'meta_key'       => 'new_used_cat',
'posts_per_page' => -1,
);

// run the query.
$query = new WP_Query( $args );

// if the query has posts...
if ( $query->have_posts() ) {
// loop through them...
while ( $query->have_posts() ) {
$query->the_post();
// default value to use as term.
$termshouldbe = 'new';
// get the current post_meta if it exists.
$meta_value = get_post_meta( $post->ID, 'new_used_cat', true );
if ( ! empty( $meta_value ) ) {
// update the value for term based on having a value for new_used_cat.
$termshouldbe = 'used';
// NOTE: you may want to delete the post_meta here so that next
// iteration this query doesn't need to work with this post
// again as it's already processed.
}
// set the term with a value (either 'new' or 'used').
wp_set_object_terms( $post->ID, $termshouldbe, 'vehicle_condition', false );
}
// restore the main query.
wp_reset_postdata();
}
} // End if().
add_action( 'prefix_save_cp_term_meta_cron', 'save_cp_term_meta_runner' );

ПРИМЕЧАНИЕ. Возможно, вы захотите добавить его с интервалами, а не в фиксированное время, или вместо этого запустить его с помощью задания cron на системном уровне. Этот ответ подробно описывает некоторые проблемы при работе с WP_Cron() это может помочь вам решить, какой метод лучше для вас: https://wordpress.stackexchange.com/a/179774/37158

0

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector