Last active

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

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.

View sane-adjacent-images.php
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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
<?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
Something went wrong with that request. Please try again.