vanpariyar/wp-post-views

Can we reset counter ?

Opened this issue · 4 comments

Hi there !
Thanks for your work. it's just perfect for my use.
Could you tell me if counter coul'd be reset (All counter or maybe by post)? and how i can reset it ?
Could you add this function to your work ?
Thanks

Well, i find in database in the postmeta table the metakey 'entry_views' a counter to reset, i reset it from database...

Could you think it's was possible to put a button to set entry_views to '0' from your Option Page ?

i'm lookking for it besause i made qr-code with the url of product-post, and your plugin help me to make inventory...

ChatGpt help me :

Certainly! You can add a button to reset the counter directly in your plugin's settings page. Here's how you can do it:

Add the following function to your Wp_post_view_settings class to display a reset button on the settings

public static function wppv_reset_counter_button() {
    ?>
    <form method="post" action="">
        <input type="hidden" name="wppv_reset_counter" value="1">
        <?php submit_button('Reset View Counter', 'primary', 'reset_counter_button'); ?>
    </form>
    <?php
}

Next, add a hook to check if the button has been clicked and reset the counter if necessary. Add this part to your wppv_api_settings_init function:

// ...
// Add this line to display the reset button
add_settings_field(
    'wppv_api_reset_counter',
    __( 'Reset View Counter', 'wppv' ),
    array( 'Wp_post_view_settings','wppv_reset_counter_button'),
    'wppvPlugin',
    'wppv_api_wppvPlugin_section'
);

// ...
// Add this condition to check if the button was clicked
if (isset($_POST['reset_counter_button']) && $_POST['wppv_reset_counter'] == 1) {
    // Reset the counter here
    global $wpdb;
    $table_name = $wpdb->prefix . 'postmeta';
    $updated = $wpdb->update($table_name, array('meta_value' => '0'), array('meta_key' => 'entry_views'));
    if ($updated) {
        echo 'The view counter has been reset successfully!';
    } else {
        echo 'An error occurred while resetting the view counter.';
    }
}

With these additions, you'll have a "Reset View Counter" button on your plugin's settings page. When you click this button, the view counter will be reset. Be sure to adjust the code to fit your specific needs. 😊

Added this to working sprint thanks.