php – Isn’t Returning Value While Using SELECT COUNT(*) FROM {$wpdb->prefix}


I’m trying to count rows in sql table using SELECT COUNT(*) for the post view count. My code is

    function get_post_views($offset=0, $post_id){
        global $wpdb;
        $get_visit_query = "SELECT COUNT(*) FROM {$wpdb->prefix}" . VISITS_TABLE .   " WHERE object_id = '{$post_id}' AND datetime > '2021-09-07 0:0:0' AND datetime < '2021-09-09 0:0:0'";
        $visits_total =  $wpdb->get_var( $get_visit_query, 0 );

        $combined_views = (int)$get_visit_query + (int)$offset;

        return $combined_views;}

And if I get the rows count value($combined_views) from VISITS_TABLE, I will change the meta_value in wp_post_meta table with the code

     function set_post_views($offset=0, $post_id) {
        $visits_total = apply_filters('get_post_views', $offset, $post_id);

        update_post_meta($post_id, POST_VIEW_COUNT_KEY, $visits_total); 

The problem is that the meta_value is 0 all the time. (Post view count is 0 all the time)
I tried this tread, but didn’t work for me sadly.

Morty_Smith 4 months 2022-04-14T08:05:48-05:00 0 Answers 0 views 0

Leave an answer