events scheduled with upda_user_meta

  php, wordpress

I am testing the scheduled events because I need to remove a meta value every so often, but I started trying something simple like sending an email and it worked for me but when I change the function to update_user_meta it does not update the user. it’s wrong?

does not work to update a meta value of the post

add_action('woocommerce_share','setPostViews',70);

function setPostViews() {

  global $product;
    
    $product_id=$product->id;

    $count_key = 'post_views_count';
    $count = get_post_meta($product_id, $count_key, true);
    if($count==''){
        $count = 0;
        delete_post_meta($product_id, $count_key);
        add_post_meta($product_id, $count_key, '0');
    }else{
       $count++;
        update_post_meta($product_id, $count_key, $count);
  
    }


echo 'view::'.$count;
}



add_filter( 'cron_schedules', 'example_add_cron_interval' );
function example_add_cron_interval( $schedules ) { 
    $schedules['five_minutes'] = array(
        'interval' => 5,
        'display'  => esc_html__( 'Every Five Minutes' ), );
    return $schedules;
}
add_action( 'woocommerce_share', 'bl_cron_exec' );
function bl_cron_exec(){

global $product;
    
     $args = array($product_id=$product->id);


if ( ! wp_next_scheduled( 'bl_cron_hook' ,$args) ) {


    wp_schedule_event( time(), 'five_seconds', 'bl_cron_hook',  $args );
}
}
add_action( 'bl_cron_hook' , 'menu_update_reminder_run' );
function menu_update_reminder_run($product_id) {


update_post_meta($product_id, 'post_views_count', 0);
      
}

it works for me to send an email



add_filter( 'cron_schedules', 'example_add_cron_interval' );
function example_add_cron_interval( $schedules ) { 
    $schedules['five_minutes'] = array(
        'interval' => 5*60,
        'display'  => esc_html__( 'Every Five Minutes' ), );
    return $schedules;
}
add_action( 'woocommerce_share', 'bl_cron_exec' );
function bl_cron_exec(){
if ( ! wp_next_scheduled( 'bl_cron_hook' ) ) {
    wp_schedule_event( time(), 'five_minutes', 'bl_cron_hook' );
}
}
add_action( 'bl_cron_hook' , 'menu_update_reminder_run' );
function menu_update_reminder_run() {

  global $product;
    
    $product_id=$product->id;

update_post_meta($product_id, 'post_views_count', 0);

}


Source: Ask PHP

LEAVE A COMMENT