• I have the following hook which works fine:

    function sr_pre_get_posts($query) {
    
    if (is_admin() and !empty($_GET["ADMIN_FILTER_FIELD_VALUE"]) and $_GET["ADMIN_FILTER_FIELD_NAME"] == "dateavailable_value") {
    
            if (isset($query->query_vars['post_type'])) {
                if ($query->query_vars['post_type'] == 'listing') {
    
    	       $query->set('orderby'  , 'meta_value' );
    	       $query->set('meta_key' , 'dateavailable_value');
    	       $query->set('order', 'ASC');
                }
            }
        }
    }
    
    add_action('pre_get_posts' , 'sr_pre_get_posts');

    However I want to sort by a different meta key called address_value which is one the fields that also appears in the results. If I change the meta_key to address_value I will get no values as it will change the query. I saw a similar question with answer:

    $query = array(
     'post_type' => 'page',
     'meta_key' => array('front_page_nav', 'front_page_nav_position'),
     'meta_value' => array('true'),
     'order_by' => array('front_page_nav_position'),
     'order' => 'ASC'
    );
    query_posts($query);

    Here the programmer wanted to sort by front_page_nav_position. Is there any way I can reproduce the same using the query->set in pre-get posts ? or how would i do the same in pre-get-posts? that is where I query by one key and sort by another.

  • The topic ‘Order by pre get posts filter’ is closed to new replies.