Quantcast
Channel: WordPress.org Forums » All Topics
Viewing all articles
Browse latest Browse all 59525

pre_get_posts hook

$
0
0

Replies: 0

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 29 minutes ago by cheater111.
  • This topic was modified 27 minutes ago by cheater111.

Viewing all articles
Browse latest Browse all 59525

Trending Articles