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.
Viewing 4 replies - 1 through 4 (of 4 total)
Viewing 4 replies - 1 through 4 (of 4 total)
- The topic ‘pre_get_posts hook’ is closed to new replies.