Custom Query in WordPress

It’s sometimes easier to run your own query than using wordpress core functions. There are pros and cons for each. Running custom query cuts through the learning curve of wordpress functions and it could actually be much more efficient. To run custom query, in your theme functions.php

add_action('whatever_wordpress_hook', 'my_db_update');
function my_db_update() {
  global $wpdb;
  $table = $wpdb->prefix.'table_name';
  // just an example query
  $query = $wpdb->prepare("update $table set access_expires=CURDATE() + INTERVAL 1 DAY where user_id='".wp_get_current_user()->ID."' and access_expires is not NULL");
  if ($wpdb->query($query) === FALSE) {
    echo "could not update $table";
  }
}
Like it.? Share it:
Tags:

Comments are closed.