Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9082565
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T20:35:21+00:00 2026-06-16T20:35:21+00:00

I have a WordPress-driven site on which I use wp-super-cache, and I am therefor

  • 0

I have a WordPress-driven site on which I use wp-super-cache, and I am therefor forced to track views by ajax and not inline PHP. The following is the guts of my PHP file that gets called via ajax. The code isn’t really WordPress specific btw, but to explain it a little bit, it updates the wp_postmeta table just like update_post_meta() does. It uses mysqli, and makes 2 sql queries to

  1. Make sure the postid to update is valid.
  2. Increment the view-count.

My question is, is there some performance issues with this code, could it be better somehow?

Excerpt of code:

  #...ajax stuff

  $json_array = array();
  $json_array["category_id"] = (int)$cat_id;
  $json_array["post_id"] = (int)$post_id;

  // get secure database credentials
  require("/server/abspath/dbinfo.inc");

  $db_details = new dbCreds;
  $mysqli = new mysqli(
    $db_details->get("dbhost"),
    $db_details->get("dbuser"),
    $db_details->get("dbpw"),
    $db_details->get("db")
  );

  # test if valid post
  # sql call no1
  $valid_post_sql = "SELECT ABS(meta_value) FROM wp_postmeta "
    . "WHERE (post_id = '" . $post_id . "' AND meta_key = 'views')";
  $current_view_count = @$mysqli->query($valid_post_sql)->fetch_array();
  $valid_post_sql_result = $current_view_count ? true : false;

  if ( $valid_post_sql_result == false ) {
    die("4");
  }

  # set new view-count
  $views = isset($current_view_count[0]) && (int)$current_view_count[0] >= 0
    ? ((int)$current_view_count[0]+1) : 0;

  # if not an admin or no views recorded yet
  if ( !$is_admin || $views == 0 ) {
    # sql call no2
    $increment_imgviews_sql = "UPDATE wp_postmeta SET meta_value = "
      . "meta_value + 1 WHERE (post_id = '" . $post_id
      . "' AND meta_key = 'views')";
    $mysqli->query($increment_imgviews_sql);
  }
  $json_array["views"] = $views;
  return $json_array;

New code:

This is what i ended up using, replacing the 2 calls with just this single one:

UPDATE wp_postmeta SET meta_value = meta_value + 1 WHERE post_id = '" . $post_id . "' AND meta_key = 'views';

Note! This makes the return response obsolete, as I don’t get the current view-count returned. But on the other hand I save a query.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T20:35:22+00:00Added an answer on June 16, 2026 at 8:35 pm

    Looks perfectly fine to me. Only thing i would say is that if this is a background task, then getting the view count response is of no use in a production environment (unless something happens im no aware of).

    So given that information, there’s no reason to validate the postid beforehand. You could drop the first sql statement along with the $views calculation, and just use the update statement (if its not a valid id, then it simply wont update anything). If you do not setup this “views” meta key when a post is created, then look at INSERT... ON DUPLICATE KEY instead of updates

    If you really want to keep your json response, then i can’t see any improvements that can really be made.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a WordPress site which have 4 pages with one page is a
I have a WordPress site and the following link is accessible: www.domain.com/wp-admin/ (obviously not
I have a wordpress site with an ajax search field that returns a list
I have wordpress a site with a customized login page, the problem is when
i have an wordpress site and it have menus over there. what i want
I have a Wordpress site (installed in root) and a Simple Machines Forum (in
I have a WordPress driven website and I need to be able to close
I have a wordpress site that i've been working on that has some pages
I have wordpress site. I have installed modern-blue-green-120070612-2 theme.Then i got an error message
I have WordPress Multi site installed and all the same sites are using one

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.