• 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 doesnt work for search. I made echo and I saw that my code is processed for search queries too, but for some reason ORDER options doesnt apply..

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)

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