• Resolved ragulanfreelance

    (@ragulanfreelance)


    Hello, I need the post views should start from 99views. How can i achieve this without any plugin customization. So it should not affect any future plugin updates. Or if there is no way without customizing the plugin then please advice me where do i need to work in the plugin to make the posts views should start from 99views ?

    Thank you!

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

    (@hcabrera)

    Hi

    There’s an action hook called wpp_pre_update_views that runs right before the plugin updates the views count of a post or a page. Maybe you can use it for this.

    I haven’t tested this code so give it a try and let me know how it goes:

    function wp5413_set_starting_views_count( $post_id, $views ) {
    
        global $wpdb;
    
        // This post/page hasn't been tracked by WPP yet, so
        // let's set its starting views count to 98 views
        // (the post count will increment +1 after this, so 
        // it'll be 99 in the end).
        if ( ! $result = $wpdb->query( "SELECT pageviews FROM {$wpdb->prefix}popularpostsdata WHERE postid = {$post_id};" ) ) {
    
            $now = WPP_helper::now();
            $curdate = WPP_helper::curdate();
    
            $wpdb->query(
                $wpdb->prepare(
                    "INSERT INTO {$wpdb->prefix}popularpostsdata (postid, day, last_viewed, pageviews) VALUES (%d, %s, %s, %d);",
                    array( $post_id, $curdate, $now, 98 )
                )
            );
    
            $wpdb->query(
                $wpdb->prepare(
                    "INSERT INTO {$wpdb->prefix}popularpostssummary (postid, pageviews, view_date, view_datetime) VALUES (%d, %d, %s, %s);",
                    array( $post_id, 98, $curdate, $now )
                )
            );
    
        }
    
    }

    Basically, this code checks if the current post already has views data stored in the database. If it doesn’t, the code will set its initial views count as 98. After this, the plugin will continue its normal execution and increment the views count +1 as it does now, giving the post/page 99 views count as requested.

    • This reply was modified 6 years, 7 months ago by Hector Cabrera. Reason: Removed code tags from query strings
    • This reply was modified 6 years, 7 months ago by Hector Cabrera. Reason: Adds link to hook wpp_pre_update_views documentation
    Thread Starter ragulanfreelance

    (@ragulanfreelance)

    Hi, No its not working. Can you please test and provide working code. here is the link i am checking the views. it shows 0 view. I added your given code and created one new post to check the views. But it shows 0. Not 99.

    https://theduran.staging.wpengine.com/test/

    Or else we can just make +99 views on the all post even if the post just created it should have 99views and if the posts already 1000 views and add +99 , so it should show 1099. whatever the solution is ok for me. So just provide the code to achieve this.

    Please check it and help me with the updated code please.

    Plugin Author Hector Cabrera

    (@hcabrera)

    Sorry, there’s a line of code that I missed there. Add add_action( 'wpp_pre_update_views', 'wp5413_set_starting_views_count', 10, 2 ); after the function and it’ll work.

    Thread Starter ragulanfreelance

    (@ragulanfreelance)

    Hello, its updated for one post. and i tried to test with another new post and views shows 0. why the views not getting updated with 99 views for newly created posts. Please check and let me know.

    Thread Starter ragulanfreelance

    (@ragulanfreelance)

    Check the screenshot for your reference.

    I checked both backend and front end. But the views not getting updated with 99 for new posts. but its updated for one post.

    Thread Starter ragulanfreelance

    (@ragulanfreelance)

    the 99 views should get updated right away once the new post is published. please help me with this

    Plugin Author Hector Cabrera

    (@hcabrera)

    No idea, man. That “Views” column in the admin section isn’t being added by the plugin so I can’t say what’s going on. Share the code you’re using to display that column and I might be able to help.

    Edit: Ah, I see what you mean now. The code I shared earlier sets the views count when someone visits the post / page. If you want to set the views count as soon as the post is created, you’ll want to use the wp_insert_post hook instead.

    Thread Starter ragulanfreelance

    (@ragulanfreelance)

    I am talking about front end too. in front end as well the views not getting updated right away. For the backend views i got the code from your Forum only.

    below are the codes i implemented to display views in backend posts page and front end post views count.

    
    
    function wp5413_set_starting_views_count( $post_id, $views ) {
    
        global $wpdb;
    
        // This post/page hasn't been tracked by WPP yet, so
        // let's set its starting views count to 98 views
        // (the post count will increment +1 after this, so 
        // it'll be 99 in the end).
        if ( ! $result = $wpdb->query( "SELECT pageviews FROM {$wpdb->prefix}popularpostsdata WHERE postid = {$post_id};" ) ) {
    
            $now = WPP_helper::now();
            $curdate = WPP_helper::curdate();
    
            $wpdb->query(
                $wpdb->prepare(
                    "INSERT INTO {$wpdb->prefix}popularpostsdata (postid, day, last_viewed, pageviews) VALUES (%d, %s, %s, %d);",
                    array( $post_id, $curdate, $now, 98 )
                )
            );
    
            $wpdb->query(
                $wpdb->prepare(
                    "INSERT INTO {$wpdb->prefix}popularpostssummary (postid, pageviews, view_date, view_datetime) VALUES (%d, %d, %s, %s);",
                    array( $post_id, 98, $curdate, $now )
                )
            );
    
        }
    
    }
    add_action( 'wpp_pre_update_views', 'wp5413_set_starting_views_count', 10, 2 );
    
    /*
     * Adds the views column to Posts Manage page
     */
    function add_wpp_views_columns($columns) {
    
        if ( function_exists('wpp_get_views') ) {
            $columns['views'] = 'Views';
        }
    
        return $columns;
    }
    add_filter('manage_posts_columns', 'add_wpp_views_columns');
    
    /*
     * Displays the views column data
     */
    function wpp_views_columns_data($name) {
    
        if ( 'views' == $name && function_exists('wpp_get_views') ) {
            global $post;
            echo wpp_get_views( $post->ID );
        }
    
    }
    add_action('manage_posts_custom_column',  'wpp_views_columns_data');
    
    • This reply was modified 6 years, 7 months ago by Jan Dembowski. Reason: Formatting
    Plugin Author Hector Cabrera

    (@hcabrera)

    Please check my previous response. And use the code button to wrap your code.

    Thread Starter ragulanfreelance

    (@ragulanfreelance)

    hello, I am not a core developer to customize this. can you please help me how to customise the insert hook with our plugin.

    
    function my_project_updated_send_email( $post_id, $post, $update ) {
    
    	// If this is a revision, don't send the email.
    	if ( wp_is_post_revision( $post_id ) )
    		return;
    
    	$post_url = get_permalink( $post_id );
    	$subject = 'A post has been updated';
    
    	$message = "A post has been updated on your website:\n\n";
    	$message .= $post->post_title . ": " . $post_url;
    
    	// Send email to admin.
    	wp_mail( '[email protected]', $subject, $message );
    }
    add_action( 'wp_insert_post', 'my_project_updated_send_email', 10, 3 );
    
    • This reply was modified 6 years, 7 months ago by Jan Dembowski. Reason: Formatting
    Plugin Author Hector Cabrera

    (@hcabrera)

    Alright, please remove the previous code and try this one instead:

    function wp5413_set_starting_views_count( $post_id, $post, $update ) {
    
        // If this is a revision, bail.
        if ( wp_is_post_revision( $post_id ) )
            return;
    
        // This is a post/page update, bail.
        if ( $update )
            return;
    
        global $wpdb;
    
        $now = WPP_helper::now();
        $curdate = WPP_helper::curdate();
        $default_views_count = 99;
    
        $wpdb->query(
            $wpdb->prepare(
                "INSERT INTO {$wpdb->prefix}popularpostsdata (postid, day, last_viewed, pageviews) VALUES (%d, %s, %s, %d);",
                array( $post_id, $curdate, $now, $default_views_count )
            )
        );
    
        $wpdb->query(
            $wpdb->prepare(
                "INSERT INTO {$wpdb->prefix}popularpostssummary (postid, pageviews, view_date, view_datetime) VALUES (%d, %d, %s, %s);",
                array( $post_id, $default_views_count, $curdate, $now )
            )
        );
    
    }
    add_action( 'wp_insert_post', 'wp5413_set_starting_views_count', 10, 3 );

    Now, whenever you create a post or a page the script above will assign it a views count of 99 automatically – even if you don’t publish the post/page right away (eg. you saved it as a draft).

    Thread Starter ragulanfreelance

    (@ragulanfreelance)

    thank you so much. its worked. You rocked!, But dont close this topic. I have to test some more in this. thank you!

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Start Views Count from 99 views’ is closed to new replies.