array( 'attachment' ) ), 'names' ); foreach( $taxonomies as $taxonomy ) { if ( empty( $my_query_vars[ $taxonomy ] ) ) { continue; } // Found the taxonomy; collect the terms $include_children = isset( $my_query_vars['include_children'] ) && 'true' == strtolower( trim( $my_query_vars['include_children'] ) ); // Allow for multiple term slug values $terms = array(); $slugs = explode( ',', $my_query_vars[ $taxonomy ] ); foreach ( $slugs as $slug ) { $args = array( 'slug' => $slug, 'hide_empty' => false ); $terms = array_merge( $terms, get_terms( $taxonomy, $args ) ); } foreach( $terms as $term ) { // Index by ttid to remove duplicates $ttids[ $term->term_taxonomy_id ] = $term->term_taxonomy_id; if ( $include_children ) { $args = array( 'child_of' => $term->term_id, 'hide_empty' => false ); $children = get_terms( 'attachment_category', $args ); foreach( $children as $child ) { $ttids[] = $child->term_taxonomy_id; } } // include_children } // $term break; } // Build an array of SQL clauses $query = array(); $query_parameters = array(); if ( $is_pagination ) { $query[] = "SELECT COUNT( DISTINCT object_id ) FROM {$wpdb->term_relationships} as tr"; } else { $query[] = "SELECT DISTINCT tr.object_id FROM {$wpdb->term_relationships} as tr"; } $placeholders = array(); if ( ! empty( $ttids ) ) { foreach ( $ttids as $ttid ) { $placeholders[] = '%s'; $query_parameters[] = $ttid; } } else { $placeholders[] = '%s'; $query_parameters[] = '0'; } $query[] = 'WHERE ( tr.term_taxonomy_id IN (' . join( ',', $placeholders ) . ') )'; // ORDER BY clause would go here, if needed if ( ! $is_pagination ) { /* * Add pagination to our query, then remove it from the query * that WordPress will process after we're done. * MLA pagination will override WordPress pagination */ if ( ! empty( $all_query_parameters['mla_paginate_current'] ) ) { if ( isset( $all_query_parameters['mla_paginate_total'] ) && ( $all_query_parameters['mla_paginate_current'] > $all_query_parameters['mla_paginate_total'] ) ) { $paged = 0xFFFF; // suppress further output } else { $paged = $all_query_parameters['mla_paginate_current']; } } else { $paged = $all_query_parameters['paged']; } if ( empty( $paged ) ) { $paged = 1; } elseif ( 'current' == strtolower( $paged ) ) { /* * Note: The query variable 'page' holds the pagenumber for a single paginated * Post or Page that includes the Quicktag in the post content. */ if ( get_query_var( 'page' ) ) { $paged = get_query_var( 'page' ); } else { $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1; } } elseif ( is_numeric( $paged ) ) { $paged = absint( $paged ); } elseif ( '' === $paged ) { $paged = 1; } $limit = absint( ! empty( $all_query_parameters['posts_per_page'] ) ? $all_query_parameters['posts_per_page'] : $all_query_parameters['numberposts'] ); $offset = $limit * ( $paged - 1); if ( 0 < $offset && 0 < $limit ) { $query[] = 'LIMIT %d, %d'; $query_parameters[] = $offset; $query_parameters[] = $limit; } elseif ( 0 < $limit ) { $query[] = 'LIMIT %d'; $query_parameters[] = $limit; } elseif ( 0 < $offset ) { $query[] = 'LIMIT %d, %d'; $query_parameters[] = $offset; $query_parameters[] = 0x7FFFFFFF; // big number! } $all_query_parameters['nopaging'] = true; $all_query_parameters['numberposts'] = 0; $all_query_parameters['posts_per_page'] = 0; $all_query_parameters['paged'] = NULL; $all_query_parameters['offset'] = NULL; $all_query_parameters['mla_paginate_current'] = NULL; $all_query_parameters['mla_paginate_total'] = NULL ; } // ! is_pagination $query = join(' ', $query); if ( $is_pagination ) { $count = $wpdb->get_var( $wpdb->prepare( $query, $query_parameters ) ); //error_log( 'MLATaxQueryExample::_query $count = ' . var_export( $count, true ), 0 ); return $count; } $ids = $wpdb->get_results( $wpdb->prepare( $query, $query_parameters ) ); if ( is_array( $ids ) ) { $includes = array(); foreach ( $ids as $id ) { $includes[] = $id->object_id; } $all_query_parameters['include'] = implode( ',', $includes ); } else { $all_query_parameters['include'] = '1'; // return no images } //error_log( 'MLATaxQueryExample::_query $all_query_parameters = ' . var_export( $all_query_parameters, true ), 0 ); return $all_query_parameters; } // _query } // Class MLATaxQueryExample /* * Install the filters at an early opportunity */ add_action('init', 'MLATaxQueryExample::initialize'); ?>