How do I make a query to return an array of stdClass objects containing the author name, sum of visits and sum of views?

0

I'm developing an author traffic plugin for a news site and I'm catching up on some queries.

I have the following widgets that are shown in the admin dashboard:

  function ws_general_statistics(){
  global $wpdb;
  $pageviews = $wpdb->get_var("SELECT SUM(siteviews_pages) FROM {$wpdb->prefix}siteviews");
  $views = $wpdb->get_var("SELECT SUM(siteviews_views) FROM {$wpdb->prefix}siteviews");
  $users = $wpdb->get_var("SELECT SUM(siteviews_users) FROM {$wpdb->prefix}siteviews");
  echo '<strong>Total de visualizações:</strong> '.($pageviews == 1 ? $pageviews. ' Visualização': $pageviews. ' Visualizações'). '<hr>';
  echo '<strong>Total de visitas:</strong> '.($views == 1 ? $views. ' Visita': $views. ' Visitas'). '<hr>';
  echo '<strong>Total de usuários:</strong> '.($users == 1 ? $users. ' Usuário': $users. ' Usuários'). '<hr>';
}

function ws_browser_statistics(){
  global $wpdb;
  $browsers = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}siteviews_agent");
  foreach ($browsers as $browser) {
    echo "<strong>{$browser->agent_name}:</strong> ".$browser->agent_views. '<hr>';
  }

}

function ws_postviews_statistics(){
  global $wpdb;
  $date = date('Y-m-d');
  $authors = $wpdb->get_results("SELECT SUM({$wpdb->prefix}siteviews_post.postview_views) AS views, SUM({$wpdb->prefix}siteviews_post.postview_pages) AS pageviews, {$wpdb->prefix}users.display_name AS name FROM {$wpdb->prefix}siteviews_post INNER JOIN {$wpdb->prefix}users ON {$wpdb->prefix}siteviews_post.postview_author_id={$wpdb->prefix}users.ID AND EXTRACT(MONTH FROM {$wpdb->prefix}siteviews_post.postview_date)=EXTRACT(MONTH FROM '{$date}')");
   //var_dump($authors);

   echo "<table id='table-authors'><thead><tr><th>Author</th><th>Visitantes</th><th>Visualizações</th></tr></thead><tboby>";
  foreach ($authors as $av) {
    echo "<tr><td>{$av->name}</td><td>{$av->views}</td><td>{$av->pageviews}</td></tr>";
  }
  echo "</tbody></table>";
}

and this is shown on the dashboard up to the author level

  function ws_general_author_statistics(){
  global $wpdb;
  global $current_user;
  wp_get_current_user();
  $post_id = $current_user->ID;
  $date = date('Y-m-d');
  //dia
  $today = $wpdb->get_var("SELECT SUM(postview_views) FROM {$wpdb->prefix}siteviews_post WHERE postview_author_id='{$post_id}' AND postview_date='{$date}'");
  //ontem
  $yesterday = $wpdb->get_var("SELECT SUM(postview_views) FROM {$wpdb->prefix}siteviews_post WHERE postview_author_id='{$post_id}' AND postview_date=('{$date}' - INTERVAL 1 DAY)");
  //semana
  $week = $wpdb->get_var("SELECT SUM(postview_views) FROM {$wpdb->prefix}siteviews_post WHERE postview_author_id='{$post_id}' AND EXTRACT(WEEK FROM postview_date)=EXTRACT(WEEK FROM '{$date}')");
  //mes
  $month = $wpdb->get_var("SELECT SUM(postview_views) FROM {$wpdb->prefix}siteviews_post WHERE postview_author_id='{$post_id}' AND EXTRACT(MONTH FROM postview_date)=EXTRACT(MONTH FROM '{$date}')");
  //ano
  $year = $wpdb->get_var("SELECT SUM(postview_views) FROM {$wpdb->prefix}siteviews_post WHERE postview_author_id='{$post_id}' AND EXTRACT(YEAR FROM postview_date)=EXTRACT(YEAR FROM '{$date}')");
  ?>
  <div id="widget-statistics-author">
    <div class="ws-box today">
      <div class="ws-box-content">
        <h2>Hoje</h2>
        <strong><span class="counter-visits"><?=($today != null ? $today : 0);?></span> <small><?=($today == 1 ? "Visita" : "VIsitas");?></small></strong>
      </div>
    </div>
    <div class="ws-box yesterday">
      <div class="ws-box-content">
        <h2>Ontem</h2>
        <strong><span class="counter-visits"><?=($yesterday != null ? $yesterday : 0);?></span> <small><?=($yesterday == 1 ? "Visita" : "VIsitas");?></small></strong>
      </div>
    </div>
    <div class="ws-box week">
      <div class="ws-box-content">
        <h2>Semana</h2>
        <strong><span class="counter-visits"><?=($week != null ? $week : 0);?></span> <small><?=($week == 1 ? "Visita" : "VIsitas");?></small></strong>
      </div>
    </div>
    <div class="ws-box month">
      <div class="ws-box-content">
        <h2>Mês</h2>
        <strong><span class="counter-visits"><?=($month != null ? $month : 0);?></span> <small><?=($month == 1 ? "Visita" : "VIsitas");?></small></strong>
      </div>
    </div>
  </div>
  <div id="statistics-year-author">
    <p>Neste ano você gerou <big><?=($year != null ? $year : 0);?> </big><?=($month == 1 ? "visita" : "visitas");?> para este site.</p>
  </div>
  <?php
}

The problem is that the ws_postviews_statistics function is to return an array of obj stdClass containing the author's name, the sum of visits and the sum of views of his posts. Except that instead, it is returning the sum of views and visits from all authors. Can you give me a light please.

Link to follow plugin development: link

User1: admin Password1: admin
User2: user Password2: user

    
asked by anonymous 19.07.2018 / 06:47

0 answers