• 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)
  • Moderator bcworkz

    (@bcworkz)

    “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

    (@cheater111)

    change add_filter on add_action – nothing changed
    About 2 – don`t understand

    Moderator bcworkz

    (@bcworkz)

    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

    (@cheater111)

    thank you, it help

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘pre_get_posts hook’ is closed to new replies.