So, I know I can make this work in a roundabout way, but I think I am doing something wrong in the first place. Essentially, the situation is like this.

I have a custom post type, the posts of which - depending on some conditions (time actually) should or should not show .

This one I can get to work quite nicely like so (somewhat abbreviated to make this less messy)

add_action( 'pre_get_posts', array( $this, 'my_tm_pre_get_posts'));

function my_tm_pre_get_posts($query){
    if (isset($query->query_vars['post_type']) && $query->query_vars['post_type']=='my_post_type') {
        $array2exclude=array(1, 2, 3); /**a conditional array depending on time of day etc)**/
        $query->set( 'post__not_in', $array2exclude );
    return $query;  

however, I also have a navigation of categories of this post type like so:

    // some_args .....
    'hide_empty'   => 1,
    'show_count'   => 1,
    // some more args...
    echo wp_list_categories( $args ); 

which displays all the right categories of this custom post type / taxonomy. So far so good.

However, if I end up excluding all post of a specific category in my_tm_pre_get_posts I would also expect (well, hope - that's what I'm trying to get to) that that particular category in wp_list_categories also gets excluded or, if the number of posts is just reduced but >0 , the count would also be reflected appropriately in the show_count....

In short, the problem that I cannot seem to figure out , is a way that reflects the excluded posts in the navigation. may that be just the count or in fact excluding a whole category if the count==0 after the pre_get_posts action

Sure, when getting the "$array2exclude" above, i could also check if that means a category would be empty and then just exclude the category from the navigation. But that doesnt really address the count in the navigation, nor do I have the feeling that's the right or most elegant way to go about this one...

Any help/hints/pointers appreciated. Happy to provide more code if required fo course. just trying to keep it simple

  • The default WordPress functions to retrieve terms, such as wp_list_categories, don't calculate the number of posts per term at runtime. You can see in the database a count column in 'wp_term_taxonomy` database table. This column is updated when terms are assigned or removed from a post. This is why your pre_get_postshas no impact. You'll need to either modify the output or create your own database calls to calculate the number of posts per category. – Seamus Leahy Dec 9 '13 at 0:45
  • thanks for the input. makes sense i guess. So would I be right in thinking then, that it would be reasonably elegant to just set the post status to "draft" for example for the items that are supposed to be "hidden" ? (or maybe a cronjob of some sort that does the same thing ?) – olly Dec 9 '13 at 1:04
  • another question (sorry): when you are saying "modify the output" do you mean changing the "walker" ? – olly Dec 9 '13 at 1:14
  • Changing the status might be the best approach to be honest; I didn't think of it. There are a few ways to modify the output. The Walker would be one. The other would be to modify through a hook the output. Or you could get the results as a string and then modify the resulting source code. – Seamus Leahy Dec 9 '13 at 1:17
  • thanks for all your input Seamus. I will sleep on that one (way too late here to do anything about that one at the moment). Generally speaking though I prefer to change things as early as possible rather than writing a function that does stuff on top of another function. Either way, you certainly gave me some ideas (and clarified things). Much appreciated – olly Dec 9 '13 at 1:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.