0

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(); ?>

1 Answer 1

1

Allright, here's how I got it working:

Rewriting the query inside a template file wouldn't work because the main query already found 0 results based on the wrong assumption that attachments should not be queried. Thus the template file doesn't even run but jumps to 404.php where everything is too late.

So one has to change the query right after it got parsed and before results were retrieved. WP provides the hook parse_query for this. See this: http://codex.wordpress.org/Plugin_API/Action_Reference http://codex.wordpress.org/Query_Overview

add_action('parse_query', 'hijack_query');

function hijack_query() {

    global $wp_query;

    // When inside a custom taxonomy archive include attachments
    if (is_tax('brands') OR is_tax('author')) {
        $wp_query->query_vars['post_type'] =  array( 'attachment' );
        $wp_query->query_vars['post_status'] =  array( null );

        return $wp_query;
    }

}

'brands' and 'author' are custom taxonomy archives showing attachments assigned to those taxonomies. Change the if statement to your needs so the query is only overwritten when necessary. Note that is_tax() returns false on category archives and tag archives: if you registered categories or tags as an attachment taxonomy, use is_category() or is_tag() respectively.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.