Title: pre_get_posts problem
Last modified: August 21, 2016

---

# pre_get_posts problem

 *  [prologuemedia](https://wordpress.org/support/users/prologuemedia/)
 * (@prologuemedia)
 * [13 years, 1 month ago](https://wordpress.org/support/topic/pre_get_posts-problem-1/)
 * I am attempting to filter a category from my homepage but when I put this code
   in my functions.php file, I get an error on the homepage:
 * function exclude_category( $query ) {
    if ( $query->is_home() && $query->is_main_query()){
   $query->set( ‘cat’, ‘-Services’ ); } }
 * add_action( ‘pre_get_posts’, ‘exclude_category’ );
 * Do you see a problem with this?

Viewing 11 replies - 1 through 11 (of 11 total)

 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [13 years, 1 month ago](https://wordpress.org/support/topic/pre_get_posts-problem-1/#post-3663497)
 * `'cat'` uses category IDs
 * [http://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters](http://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters)
 *  Thread Starter [prologuemedia](https://wordpress.org/support/users/prologuemedia/)
 * (@prologuemedia)
 * [13 years, 1 month ago](https://wordpress.org/support/topic/pre_get_posts-problem-1/#post-3663507)
 * Ok. If I replace it with this:
 * function exclude_category( $query ) {
    if ( $query->is_home() && $query->is_main_query()){
   $query->set( ‘cat’, ‘-4’ ); } }
 * add_action( ‘pre_get_posts’, ‘exclude_category’ );
 * I still get an error.
 *  [Deepak Rajpal](https://wordpress.org/support/users/deepakrajpal/)
 * (@deepakrajpal)
 * [13 years, 1 month ago](https://wordpress.org/support/topic/pre_get_posts-problem-1/#post-3663530)
 *     ```
       function exclude_category( $query ) {
       if ( $query->is_home() && $query->is_main_query() ) {
       $query->set( 'cat', '-4' );
       }
       }
       ```
   
 * add_action( ‘pre_get_posts’, ‘exclude_category’ );`
 * Your code above works fine. By the way what error you are getting.
 *  Thread Starter [prologuemedia](https://wordpress.org/support/users/prologuemedia/)
 * (@prologuemedia)
 * [13 years, 1 month ago](https://wordpress.org/support/topic/pre_get_posts-problem-1/#post-3663547)
 * To be honest, I am not sure. I am just getting an Error 500 on the homepage.
 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [13 years, 1 month ago](https://wordpress.org/support/topic/pre_get_posts-problem-1/#post-3663549)
 * Try checking your site’s error logs for a more specific error message.
 *  Thread Starter [prologuemedia](https://wordpress.org/support/users/prologuemedia/)
 * (@prologuemedia)
 * [13 years, 1 month ago](https://wordpress.org/support/topic/pre_get_posts-problem-1/#post-3663552)
 * Ok, I figured out the error (The function was being declared twice). Now the 
   page loads but, it is not excluding the category on the page as I had hoped.
 *  [Deepak Rajpal](https://wordpress.org/support/users/deepakrajpal/)
 * (@deepakrajpal)
 * [13 years, 1 month ago](https://wordpress.org/support/topic/pre_get_posts-problem-1/#post-3663555)
 * Are you sure “4” is the cat id for “Services”?
 *  Thread Starter [prologuemedia](https://wordpress.org/support/users/prologuemedia/)
 * (@prologuemedia)
 * [13 years, 1 month ago](https://wordpress.org/support/topic/pre_get_posts-problem-1/#post-3663567)
 * I changed the code to this:
 * function exclude_category( $query ) {
    if ( $query->is_home() && $query->is_main_query()){
   $query->set( ‘category_name=services’ ); } } add_action( ‘pre_get_posts’, ‘exclude_category’);
 * And the posts still appear.
 *  [bob.passaro](https://wordpress.org/support/users/bobpassaro/)
 * (@bobpassaro)
 * [13 years, 1 month ago](https://wordpress.org/support/topic/pre_get_posts-problem-1/#post-3663622)
 * Hi,
 * I posted about this very issue here today as well:
 * [http://wordpress.org/support/topic/trouble-with-pre_get_posts](http://wordpress.org/support/topic/trouble-with-pre_get_posts)
 * There is code there that shows a solution. I don’t however know why. The code
   as shown in the [Codex for this](http://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts)
   seems to be not working.
 * The revised code is from Justin Tadlock at ThemeHybrid. He seemed to suggest 
   he’s also been having trouble with pre_get_posts as used in the Codex example.
 *  [Deepak Rajpal](https://wordpress.org/support/users/deepakrajpal/)
 * (@deepakrajpal)
 * [13 years, 1 month ago](https://wordpress.org/support/topic/pre_get_posts-problem-1/#post-3663670)
 *     ```
       function exclude_category( $query ) {
       if ( $query->is_home() && $query->is_main_query() ) {
       $query->set( 'category_name=services' );
       }
       }
       add_action( 'pre_get_posts', 'exclude_category' );
       ```
   
 * Above code is not proper and there must be getting warning:
    (Warning: Missing
   argument 2 for WP_Query::set())
 * Better to use category id for excluding such as:
 *     ```
       function exclude_category( $query ) {
       	if ( $query->is_home() && $query->is_main_query() ) {
       	$query->set( 'cat', '-5,-4' );
       	}
       }
       add_action( 'pre_get_posts', 'exclude_category' );
       ```
   
 *  [PinkishHue](https://wordpress.org/support/users/pinkishhue/)
 * (@pinkishhue)
 * [12 years, 5 months ago](https://wordpress.org/support/topic/pre_get_posts-problem-1/#post-3663901)
 * Thank you Deepak Rajpal, your post above is working for me – I have changed is_home
   to is_tag for my purposes – however – it doesn’t work if I add a tag slug as 
   well such as is_tag(‘orange’) – any suggestions?
 * Many thanks 🙂

Viewing 11 replies - 1 through 11 (of 11 total)

The topic ‘pre_get_posts problem’ is closed to new replies.

## Tags

 * [pre_get_posts](https://wordpress.org/support/topic-tag/pre_get_posts/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 11 replies
 * 6 participants
 * Last reply from: [PinkishHue](https://wordpress.org/support/users/pinkishhue/)
 * Last activity: [12 years, 5 months ago](https://wordpress.org/support/topic/pre_get_posts-problem-1/#post-3663901)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
