Title: wp_query (pre_get_posts) issue
Last modified: August 21, 2016

---

# wp_query (pre_get_posts) issue

 *  [gnollik](https://wordpress.org/support/users/gnollik/)
 * (@gnollik)
 * [12 years, 8 months ago](https://wordpress.org/support/topic/wp_query-pre_get_posts-issue/)
 * Hi, I got such issue – wp_query editing doesn`t work for search queries. The 
   code:
 *     ```
       function my_modify_main_query($query)
       {
       	if(is_category() || is_search())
       	{
       		$sorting = $_GET['sorting'];
   
       		session_start();
   
       		if($sorting != '')
       		{
       			$_SESSION['sorting'] = $sorting;
       		}
       		else
       		{
       			$sorting = $_SESSION['sorting'];
       		}
   
       		if($sorting == 'latest')
       		{
       			$query->set('orderby', 'date');
       			$query->set('order', 'DESC');
       		}
   
       		if($sorting == 'price-l-h')
       		{
       			$query->set('meta_key', 'amzn_FlatPrice');
       			$query->set('orderby', 'meta_value');
       			$query->set('order', 'ASC');
       		}
   
       		if($sorting == 'price-h-l')
       		{
       			$query->set('meta_key', 'amzn_FlatPrice');
       			$query->set('orderby', 'meta_value');
       			$query->set('order', 'DESC');
       		}
       	}
       }
   
       add_action( 'pre_get_posts', 'my_modify_main_query' );
       ```
   
 * All works fine for categories but doesn`t work for search. I made echo and I 
   saw that my code is processed for search queries too, but for some reason ORDER
   options doesn`t apply..

Viewing 1 replies (of 1 total)

 *  [jorghe](https://wordpress.org/support/users/jorghe/)
 * (@jorghe)
 * [12 years, 8 months ago](https://wordpress.org/support/topic/wp_query-pre_get_posts-issue/#post-4176994)
 * You have to return the query at the end of the function. Or, at least, I saw 
   an example doing so.

Viewing 1 replies (of 1 total)

The topic ‘wp_query (pre_get_posts) issue’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 1 reply
 * 2 participants
 * Last reply from: [jorghe](https://wordpress.org/support/users/jorghe/)
 * Last activity: [12 years, 8 months ago](https://wordpress.org/support/topic/wp_query-pre_get_posts-issue/#post-4176994)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
