Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I think this is a fairly simple issue but I haven't got my head around it.

Question: how do I get my custom posts to show only on the category archives, but not in the blog post listing page?

Background: I have a custom post type "video", with has_archive true and 'taxonomies' => array('category', 'post_tag') - I want to use standard categories with this custom post type.

I have successfully got my category pages to display by setting post_type and using the hook pre_get_posts as follows:

function my_query_post_type($query) {
    if ( is_home() && false == $query->query_vars['suppress_filters'] )
        $query->set( 'post_type', array( 'post', 'video', 'attachment' ) );
    return $query;
}
add_filter('pre_get_posts', 'my_query_post_type');

If I don't use pre_get_posts, then my videos don't show - as expected.

However, now my blog post listing page shows the custom posts - as expected because I've changed the post_type in query.

Question: how do I get my custom posts to show only on the category archives, but not in the blog post listing page?

share|improve this question
    
Weird that this works for category pages - on a category archive is_home() will be false, no? –  Jan Fabry May 27 '11 at 18:18
    
It is, but it does - I don't understand why! –  AMcDermott May 27 '11 at 18:58
add comment

2 Answers

up vote 3 down vote accepted

This may be overly simplistic, but have you tried changing is_home() to is_category(), in this line:

if ( is_home() && false == $query->query_vars['suppress_filters'] )

i.e. change it to this:

if ( is_category() && false == $query->query_vars['suppress_filters'] )

Worth a shot, anyway...

share|improve this answer
    
Wonderfully simple, yes, this works. –  AMcDermott May 27 '11 at 18:58
    
what is the $query->query_vars['suppress_filters'] for? –  Bakaburg Oct 15 '12 at 23:34
    
@Bakaburg that's a question that probably merits its own Question. –  Chip Bennett Oct 16 '12 at 14:52
add comment

This is not easy to explain, but if someone else sees the issue they will be thankful, as I didn't catch this bug until months later when adding some other customization. I found out that !is_admin() was needed to prevent triggering this query in the administration page. An error was occurring when I filtered twice in a row on Category to toggle between posts from two different categories. The error would not occur when I only filtered once. It would always come up on the second attempt to filter. This toggling between categories was important for me as I added a couple extra quick edit fields to dealing with sorting of posts.

So I added !is_admin() which eliminated the issue.

is_category() && (!is_admin()) && false == $query->query_vars['suppress_filters'] 

my URL was the following ugly thing after the second time of trying to filter on a category for the second time:

edit.php?post_status=all&post_type=Array&_wpnonce=09d0e899fa&_wp_http_referer=%2Fironglasslighting.org%2Fwp-admin%2Fedit.php%3Fs%26post_status%3Dall%26post_type%3Dproduct%26action%3D-1%26m%3D201112%26cat%3D31%26paged%3D1%26mode%3Dlist%26action2%3D-1&action=-1&m=201112&cat=27&paged=1&mode=list&action2=-1

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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