Skip to content

Instantly share code, notes, and snippets.

@markjaquith
Last active December 10, 2015 01:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save markjaquith/4357848 to your computer and use it in GitHub Desktop.
Save markjaquith/4357848 to your computer and use it in GitHub Desktop.
Makes adjacent image queries saner (i.e. doesn't query ALL OF THEM). Useful if you have a huge number of images attached to a post.
<?php
/*
Plugin Name: Sane Adjacent Images queries
Description: Makes adjacent image queries saner if you have a huge number of images attached to a post.
Version: 0.2
Author: Mark Jaquith
Author URI: http://coveredwebservices.com/
*/
class CWS_Sane_Adjacent_Images {
static $instance;
private $cache = array();
public function __construct() {
self::$instance = $this;
add_action( 'init', array( $this, 'init' ) );
}
public function init() {
add_action( 'parse_query', array( $this, 'parse_query' ) );
}
protected function set( $id, $trio ) {
$this->cache[ absint( $id ) ] = $trio;
}
protected function get( $id ) {
$id = absint( $id ) ;
if ( isset( $this->cache[ $id ] ) )
return $this->cache[ $id ];
else
return false;
}
public function parse_query( $q ) {
global $wpdb;
$vars = &$q->query_vars;
if (
isset( $vars['post_parent'] ) &&
$vars['post_parent'] &&
isset( $vars['orderby'] ) &&
'menu_order ID' == $vars['orderby'] &&
isset( $vars['post_type'] ) &&
'attachment' == $vars['post_type'] &&
isset( $vars['post_mime_type'] ) &&
'image' == $vars['post_mime_type'] &&
isset( $vars['numberposts'] ) &&
-1 == $vars['numberposts']
) {
// Backtrace to make sure we're only using this when called from adjacent_image_link()
$backtrace = debug_backtrace();
if ( !isset( $backtrace[8]['function'] ) || 'adjacent_image_link' !== $backtrace[8]['function'] )
return;
$post = get_post();
if ( $post ) {
if ( ! $includes = $this->get( $post->ID ) ) {
$includes = array();
$query = "SELECT ID FROM {$wpdb->posts} WHERE {$wpdb->posts}.post_parent = %d AND {$wpdb->posts}.post_type = 'attachment' AND {$wpdb->posts}.post_mime_type LIKE %s AND ( {$wpdb->posts}.menu_order %%DIRECTION%% %d OR {$wpdb->posts}.ID %%DIRECTION%% %d ) ORDER BY {$wpdb->posts}.menu_order, {$wpdb->posts}.ID ASC LIMIT 1";
foreach ( array( '>', '<' ) as $direction ) {
// var_dump( $wpdb->prepare( str_replace( '%%DIRECTION%%', $direction, $query ), $vars['post_parent'], 'image/%', $post->menu_order, $post->ID ) );
$result = $wpdb->get_var( $wpdb->prepare( str_replace( '%%DIRECTION%%', $direction, $query ), $vars['post_parent'], 'image/%', $post->menu_order, $post->ID ) );
if ( is_numeric( $result ) )
$includes[] = absint( $result );
}
// var_dump( $includes );
$this->set( $post->ID, $includes );
}
// Always add the "center" attachment
$includes[] = absint( $post->ID );
// Now we limit the query to just have the "center" attachment and 0-2 adjacent posts
$q->set( 'post__in', $includes );
}
}
}
}
new CWS_Sane_Adjacent_Images;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment