Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

executable file 36 lines (32 sloc) 1.276 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
<?php
/*
Plugin Name: wp_fasterquery
Description: Fix Wordpress' default naive post query to avoid trashing your DB with huge temp tables
Version: 1.0
Author: Chris Heald
*/

$saved_posts_count = 0;
$last_order_clause = null;
function rewrite_posts_queries_to_use_ids( $input ) {
  global $wpdb, $last_order_clause;
  preg_match("/ORDER BY (.*? (ASC|DESC))(,.*? (ASC|DESC),?)*/i", $input, $matches);
  $last_order_clause = $matches[0];
  $newquery = str_replace("{$wpdb->posts}.* FROM {$wpdb->posts}", "{$wpdb->posts}.ID FROM {$wpdb->posts}", $input);
  return $newquery;
}

function find_posts_from_ids($posts) {
  global $wpdb, $saved_posts_count, $last_order_clause;
  $ids = array();
  if(is_array($posts)) {
    foreach($posts as $post) { $ids[] = $post->ID; }
  }
  $saved_posts_count = $wpdb->get_var( 'SELECT FOUND_ROWS()' );
  return $wpdb->get_results("SELECT * FROM {$wpdb->posts} WHERE ID IN (" . implode(",", $ids) . ") $last_order_clause;");
}

function use_save_found_post_count() {
  global $saved_posts_count;
  return $saved_posts_count;
}

add_filter( 'posts_request', 'rewrite_posts_queries_to_use_ids' );
add_filter( 'posts_results', 'find_posts_from_ids' );
add_filter( 'found_posts', 'use_save_found_post_count' );
Something went wrong with that request. Please try again.