• Resolved nikphirke

    (@nikphirke)


    I’m developing a theme and is looking for a direction to how could I delete/reset custom field value to zero on weekly basis (On sunday).

    I’ve tried the following code for post_views_count custom field but it’s not working. Any idea?

    function hits_reset_zero_schedule() {
      if ( ! wp_next_scheduled( 'hits_reset_to_zero') )
        wp_schedule_event( time(), 'weekly', 'hits_reset_zero' );
    }
    add_action( 'wp', 'hits_reset_zero_schedule' );
    
    function hits_reset_zero_func() {
      delete_post_meta( $post_id, 'post_views_count', true );
    }
    add_action( 'hits_reset_zero', 'hits_reset_zero_func' );
    
Viewing 2 replies - 1 through 2 (of 2 total)
  • Hi @nikphirke!

    If you want to delete post_views_count from all posts, you could use the delete_post_meta_by_key() function, like so:

    delete_post_meta_by_key( 'post_views_count' )

    If you only want to delete it from specific posts, you can use your current functionality, but you’ll need to define the correct value for $post_id.

    Thread Starter nikphirke

    (@nikphirke)

    Hi Gary, thanks for the reply. Using delete_post_meta_by_key() worked as I wanted.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Delete/reset custom field value weekly’ is closed to new replies.