Title: pre_get_posts hook
Last modified: November 17, 2018

---

# pre_get_posts hook

 *  [Chirukin Bogdan](https://wordpress.org/support/users/cheater111/)
 * (@cheater111)
 * [7 years, 6 months ago](https://wordpress.org/support/topic/pre_get_posts-hook/)
 * Hi to all, on my WP-site I created filter of posts. if use pre_get_posts hook:
 *     ```
       function filter($query){
           $get = $_SERVER['REQUEST_URI'];
   
       $f = explode('?', $get);
       if($f[1] && is_main_query()){
           $fil = explode('&', $f[1]);
           $g = explode('=', $fil[0]);
           $y = explode('=', $fil[1]);
           $b = explode('=', $fil[2]);
           if($g[1] !== 'all'){
       		$tax[] = array('taxonomy'=>'genres','terms'=>$g[1],'field'=>'id');
           }
           if($y[1] !== 'all'){
       		$tax[] = array('taxonomy'=>'dtyear','terms'=>$y[1],'field'=>'name');
           }
           if(!empty($tax)){
       		$query->set('tax_query', $tax);        
           }
           if($b[1] !== 'all'){
               if($b[1] === 'az'){
                   $query->set('orderby', array('title'=>'ASC'));
               }
               if($b[1] === 'za'){
                   $query->set('orderby', array('title'=>'DESC'));
               }
               if($b[1] === 'dateno'){
                   $query->set('orderby', array('date'=>'DESC'));
               }
               if($b[1] === 'dateon'){
                   $query->set('orderby', array('date'=>'ASC'));
               }
               if($b[1] === 'ratingbl'){
           		$query->set('orderby', 'meta_value_num');	
           		$query->set('meta_key', 'imdbRating');	 
           		$query->set('order', 'DESC'); 
               }
               if($b[1] === 'ratinglb'){
           		$query->set('orderby', 'meta_value_num');	
           		$query->set('meta_key', 'imdbRating');	 
           		$query->set('order', 'ASC');
               }
           }
       }
       return $query;
       }
   
       add_filter('pre_get_posts', 'filter' );
       ```
   
 * filter work right, but when array tax is not empty – nav menu disappear. Whith
   orderby this problem absent. I hope that you can help me.
    -  This topic was modified 7 years, 6 months ago by [Chirukin Bogdan](https://wordpress.org/support/users/cheater111/).
    -  This topic was modified 7 years, 6 months ago by [Chirukin Bogdan](https://wordpress.org/support/users/cheater111/).

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

 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [7 years, 6 months ago](https://wordpress.org/support/topic/pre_get_posts-hook/#post-10891416)
 * “pre_get_posts” is an action, not a filter, you should be using add_action(),
   not add_filter(). The two are closely related, which is why add_filter() still
   works. It’s important to maintain the distinction anyway, the way a callback 
   is supposed to work in each case is very different. For example, you will find
   there is no point in returning the passed $query object. In filter callbacks,
   failing to do so would break the filter’s functionality.
 * Where you check is_main_query(), you need to use the passed object’s method, 
   not the global function.
    `if ( $f[1] && $query->is_main_query()) {`
 *  Thread Starter [Chirukin Bogdan](https://wordpress.org/support/users/cheater111/)
 * (@cheater111)
 * [7 years, 6 months ago](https://wordpress.org/support/topic/pre_get_posts-hook/#post-10891961)
 * change add_filter on add_action – nothing changed
    About 2 – don`t understand
    -  This reply was modified 7 years, 6 months ago by [Chirukin Bogdan](https://wordpress.org/support/users/cheater111/).
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [7 years, 6 months ago](https://wordpress.org/support/topic/pre_get_posts-hook/#post-10893029)
 * Going to add_action() wouldn’t have changed behavior, it’s just proper convention
   to use add_action() for actions.
 * Replace your line
    `if($f[1] && is_main_query()){`
 * with this:
    `if ( $f[1] && $query->is_main_query()) {`
 * Your version is checking main query status of the wrong query (the global $wp_query
   object), you want to be checking that of the query passed to your callback.
 *  Thread Starter [Chirukin Bogdan](https://wordpress.org/support/users/cheater111/)
 * (@cheater111)
 * [7 years, 6 months ago](https://wordpress.org/support/topic/pre_get_posts-hook/#post-10894037)
 * thank you, it help

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

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

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 4 replies
 * 2 participants
 * Last reply from: [Chirukin Bogdan](https://wordpress.org/support/users/cheater111/)
 * Last activity: [7 years, 6 months ago](https://wordpress.org/support/topic/pre_get_posts-hook/#post-10894037)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
