• Resolved a4jp

    (@a4jpcom)


    I want to be able to easily edit the number of views as sometimes when I duplicate pages the number of views is also duplicated. Can you add a setting so we can edit the numbers directly on pages please? Also on some sites I’ve noticed bot spam increases the number of views.

    The page I need help with: [log in to see the link]

Viewing 11 replies - 1 through 11 (of 11 total)
  • Plugin Author Hector Cabrera

    (@hcabrera)

    Hi @a4jpcom,

    … sometimes when I duplicate pages the number of views is also duplicated

    That shouldn’t be possible unless the plugin you’re using to duplicate pages also interacts with WPP’s database tables? How can you tell that views are being “duplicated” exactly?

    Can you add a setting so we can edit the numbers directly on pages please?

    I’ll keep this idea in mind, no promises though.

    Also on some sites I’ve noticed bot spam increases the number of views.

    Most spam bots don’t execute any JavaScript code so it must be a specific bot or bots doing this. You’ll need to provide more information (eg. a way to identify which bot(s) is/are responsible for this) so that I may be able to assist.

    Alternatively, you could also set up your site so it uses Cloudflare. Cloudflare can filter out bot traffic automatically for you. Or find a similar service if you don’t want to use Cloudflare, the point is to have something between your sites and the Internet that can identify and keep bots from ever reaching them.

    Thread Starter a4jp

    (@a4jpcom)

    I made a plugin to show the number of views from your plugin for pages and posts. I just don’t know how to let it edit the number. If you can help or add this to your plugin it would be great.

    <?php
    /**
     * Plugin Name: Glen Custom View Count
     * Description: Add a view count field to pages and posts.
     * Version: 1.0
     * Author: Glen Rowell
     */
    
    // Function to get the post views count using WordPress Popular Posts
    function custom_get_wpp_post_views($postID) {
        return do_shortcode('[wpp_views_count post_id=' . $postID . ']');
    }
    
    // Function to add custom view count meta box to the right side of the post editor
    function custom_view_count_metabox() {
        add_meta_box(
            'custom-view-count-metabox',
            'Page/Post View Count',
            'custom_view_count_metabox_callback',
            'post',
            'side',
            'default'
        );
    }
    add_action('add_meta_boxes_post', 'custom_view_count_metabox');
    
    // Callback function to display the "WordPress Popular Posts" view count in the meta box
    function custom_view_count_metabox_callback($post) {
        $postID = $post->ID;
    
        // Get the "WordPress Popular Posts" view count
        $count_wpp = custom_get_wpp_post_views($postID);
    
        // Remove commas and the word "views" from the count for the input field
        $count_wpp_cleaned = str_replace([' ', ',', 'views'], '', $count_wpp);
    
        // Output the view count field
        ?>
        <label for="custom-view-count">Page/Post View Count:</label>
        <input type="text" id="custom-view-count" name="custom_view_count" value="<?php echo esc_attr($count_wpp_cleaned); ?>">
        <p><?php echo esc_html($count_wpp); ?></p>
        <?php
    }
    
    // Save the custom view count when the post is saved
    function save_custom_view_count($post_id) {
        if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
    
        if (isset($_POST['custom_view_count'])) {
            $view_count = absint($_POST['custom_view_count']);
            update_post_meta($post_id, '_custom_view_count', $view_count);
        }
    }
    add_action('save_post', 'save_custom_view_count');
    ?>
    Thread Starter a4jp

    (@a4jpcom)

    The number of views shows when you edit the post or page at the bottom of the settings menu on the right side.

    Plugin Author Hector Cabrera

    (@hcabrera)

    Ah, then I guess that what’s being duplicated is your _custom_view_count meta field. If so then this is the field that you need to edit after duplicating a post.

    Thread Starter a4jp

    (@a4jpcom)

    I could see the number of views was high for new pages from your plugin on the right side of my site. But if I can get this plugin/code working it doesn’t matter anymore as I can easily fix anything. And adjust pages/posts if something weird happens. I just want to see the real numbers but sometimes groups of people spam click to get their page view numbers higher. I’m okay with that but want a bit of control in some situations or if I’m making mockup sites for clients. If we can set the numbers it would be useful.

    Plugin Author Hector Cabrera

    (@hcabrera)

    I’m not sure what you’re asking here. Do you need help getting that code you posted earlier to work?

    Thread Starter a4jp

    (@a4jpcom)

    Yes. I want to request a setting to change the number of page/post views or to get help to get the code I posted to work. You can freely use it anyway you like as well. Anything that helps people is great.

    Plugin Author Hector Cabrera

    (@hcabrera)

    Well, I already said I’d think about it. If this is something you need urgently though then it might be best to just hire a dev or reach out to the people who maintain your websites -assuming it’s not you- and ask them to assist with your code.

    Thread Starter a4jp

    (@a4jpcom)

    I do everything by myself. That’s why I wanted to find out how to change/save the numbers from you as you made the plugin. Even if you could point me to the code that saves the page views that would help a lot.

    Thread Starter a4jp

    (@a4jpcom)

    Can you give some coding hints on how to change the numbers? Are you just saving one number per page or multiple numbers?

    Plugin Author Hector Cabrera

    (@hcabrera)

    Hey @a4jpcom,

    So the plugin stores views in two ways:

    • Total views count per post ID, this is saved to the wp_popularpostsdata table.
    • Singles views, which are saved to the wp_popularpostssummary table. This table is what allows the plugin to, for example, list the most viewed posts from the past 7 days or the last hour or even the last 5 minutes.

    This “views editing” functionality you’re developing would need to find a way to edit both tables while keeping views count in sync (unless you don’t care that there’s a mismatch between the two).

    I highly suggest considering hiring a developer to assist you with this so everything works exactly the way you/your client expects it to.

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Duplicated/cloned pages have the same view count. How do I edit the view count?’ is closed to new replies.