This might sound like query_posts with pagination problem number 2103024 but I think this one might be special.
My WordPress site is basically a huge database of car images. Each image should have tags like 'color' => 'red' but a set of images should be grouped in a gallery with certain other tags like 'location' => 'Monaco' (=> all images in this set are taken in Monaco). I should be able to create an archive for each image tag e.g. /color/red and I'd see all images showing red cars. I should be able to create an archive for each gallery tag e.g. /location/Monaco and see all galleries taken in Monaco.
I thought long about this and I decided to use WordPress' post-attachment function in combination with custom taxonomies. There are several plugins that allow you to attach custom taxonomies to attachments and it's easy for the user who can simply use batch upload to push 30 images to the server. So an attachment serves as an image and images attached to a custom post type serve as a gallery.
I got it working pretty much exactly as I wanted but now I'm stuck with the custom taxonomy archive for the attachments. In order to make WP show me attachments I have to push two parameters to the original query:
global $wp_query;
// convert query object to array
$original_query = (array) $wp_query;
// attach query parameters to original query
$attach_query = array(
'post_type'=> array( 'attachment' ),
'post_status' => array( null )
);
// merge both arrays
$args = array_merge($original_query['query_vars'], $attach_query);
query_posts( $args );
That's it, /image-tags/black/ is showing me a list of black cars. I even have the pagination working properly. However /image-tags/black/page/2/ and higher throw an error 404, strangely while still showing me my desired attachments. I wasn't event noting it first since one can only tell from the page title and body classes showing that this is clearly the wrong template location.
Below I'm posting my complete index.php and print_r of global $wp_query. As you can see the query object shows a 'max_num_pages' value of 2.
Any ideas will be greatly appreciated!
print of Query Object on pastebin
<?php
get_header();
global $wp_query;
// convert query object to array
$original_query = (array) $wp_query;
// attach query parameters to original query
$attach_query = array(
'post_type'=> array( 'attachment' ),
'post_status' => array( null )
);
// merge both arrays
$args = array_merge($original_query['query_vars'], $attach_query);
query_posts( $args );
?>
<?php if ( have_posts()) : ?>
<div class="wrap image-list">
<?php while ( have_posts() ) : the_post(); ?>
<div class="image">
<a href="<?php the_permalink(); ?>">
<div class="caption"><h3><?php the_title(); ?></h3></div>
<?php
echo wp_get_attachment_image($attachment->ID, 'bigger-thumb');
?>
</a>
</div>
<?php endwhile; ?>
</div><!-- .wrap.image-list -->
<div class="wrap pagination">
<?php
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages
) );
?>
</div><!-- .pagination.wrap -->
<?php else: ?>
</div><!-- .wrap.image-list -->
<p class="wrap"><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
<?php get_footer(); ?>