0

Per this question (How to create an attachments archive with working pagination?), I've been using the following function to have archive pages display attachments as well as pages and posts. It was working for the preexisting tag and category archive pages, but it stopped working a few weeks ago (possibly due to a Wordpress update, but I didn't notice at the time). I checked, and the post_tag and category taxonomies are still registered for attachments.

This code still works to display attachments in the archive pages of custom taxonomies (topic and training below). Any ideas on what changed, and what to change to get this working again?

add_action('parse_query', 'hijack_query');
function hijack_query() {
    global $wp_query;
    // When inside a custom taxonomy archive, include attachments AS WELL AS pages and posts.
    // Note that is_tax() returns false on category archives and tag archives. Use is_category() and is_tag() respectively when checking for category and tag archives.

    if (is_tax('topic') OR is_tax('training') OR is_tag() OR is_category()) {
        $wp_query->query_vars['post_type'] =  array( 'attachment', 'page', 'post' );
        $wp_query->query_vars['post_status'] =  array( null );
        return $wp_query;
    }
}

2 Answers 2

1

The WP_Query documentation on the codex specifically says:

'attachment' - an attachment. Whilst the default WP_Query post_status is 'publish', attachments have a default post_status of 'inherit'. This means no attachments will be returned unless you also explicitly set post_status to 'inherit' or 'any'.

I am not sure why it was working before because this is not a recent change but it looks like post_status would have to be 'inherit' or 'any' rather than null.

3
  • Hm, unfortunately changing post_status to 'inherit' stops the tag archive page from displaying posts as well, and 'any' gives the same result as null does. Also, I want to emphasize that this still works perfectly on archive pages for custom taxonomies.
    – tshynik
    May 7, 2015 at 18:34
  • Interesting. What do your template pages look like for the category/tag archive templates? Could there be something there that is overriding it after the fact?
    – ambroseya
    May 7, 2015 at 18:57
  • Thank you for your response! No, no custom archive template.
    – tshynik
    May 7, 2015 at 19:15
0

Figured it out! The culprit was the plugin "Add Categories to Pages" which one of my editors had added to the site. In addition to registering post_tag and category for the page post type, it also modified the archive.php and tag.php queries to only look in the post and page types---yet another solution for the issue of including posts, pages, and attachments:

// Add Page as a post_type in the archive.php and tag.php 
function category_and_tag_archives( $wp_query ) {

    $my_post_array = array('post','page');

    if ( $wp_query->get( 'category_name' ) || $wp_query->get( 'cat' ) )
      $wp_query->set( 'post_type', $my_post_array );

    if ( $wp_query->get( 'tag' ) )
      $wp_query->set( 'post_type', $my_post_array );
  }

AND ANOTHER THING---make sure to visit the permalink options page (yoursite.com/wp-admin/options-permalink.php) after adding a new taxonomy. Saving, even without making changes, will flush the rewrite rules. Viewing the new taxonomy archive will return a 404 until then.

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.