0

I was using query for excluding categories from my homepage without problems until this week. I'd like to know the reason of the change and a better way to get this. I was trying also diverse solutions that I found here, no success.

function excludeCat($query) {
  if ( $query->is_home() ) {
    $query->set('cat', '-99, -263, -255, -268, -105');
  }
  return $query;
}
add_filter('pre_get_posts', 'excludeCat');

I also tried this in the index. No results.

if ( is_home() ) {
query_posts( 'cat=-99, -263, -255, -268, -105' );
}

updated: [the loop in the index: homepage.php]

<?php query_posts( array( 'posts_per_page' => $homePortfolioShow ) ); 
rewind_posts();
if (have_posts()) :
while (have_posts()) : the_post();
if(!get_post_format()) {
get_template_part('format', 'standard');
} else {
get_template_part('format', get_post_format());
}
endwhile;
endif;
?>
1

I have no idea how this was ever working for you to begin with, but there's really no need to use a custom page template for the blog posts index template. WordPress provides a template file to use: home.php. Easiest solution:

  1. Rename homepage.php as home.php and remove any custom template header comments
  2. Remove all query_posts() references inside home.php
  3. Ensure that Posts Page settings are correct in Settings -> Reading
  4. Keep pre_get_posts filter in place

This will revert WordPress to using the reserved template, home.php, to render the blog posts index, while allowing WordPress to recognize the query as being the blog posts index.

|improve this answer|||||
  • There is another bug in his code, I guess. pre_get_posts is action and not filter. – Krzysiek Dróżdż Nov 19 '13 at 21:15
  • ok, thank you guys very much. I'll try Chip's suggestion. And @KrzysiekDróżdż, you're right. I'll work on that. Thank you guys again – cordltx Nov 19 '13 at 21:22
  • True enough, though, at their core, actions are really just filters themselves; so, technically, calling add_filter() probably works anyway. – Chip Bennett Nov 19 '13 at 23:03

Your Answer

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

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