Skip to main content
3 of 4
added 344 characters in body

try to return the $wp_query when is not admin

add_action('pre_get_posts', 'query_custom_admin_search', 21  );
function query_custom_admin_search( $wp_query ) {
    global $current_user;
    if (current_user_can('administrator'){
        if ( $wp_query->query['post_type'] != "apartments" ){
            return;
        }else{
            $custom_fields = array("city","state_county",);
            $search_term = $wp_query->query_vars['s'];
            if ( $search_term != '' ) {
                $meta_query = array( 'relation' => 'OR' );
                foreach( $custom_fields as $custom_field ) {
                    array_push( $meta_query, array(
                        'key' => $custom_field,
                        'value' => $search_term,
                        'compare' => 'LIKE'
                    ));
                }
                $wp_query->set( 'meta_query', $meta_query );
            }
            return;
        }
    }else{
        return $wp_query;
    }
}

I'm doing this without testing, but a few things about the code, why are you using is_admin() and then checking if it is administrator, if this is on the website, it is not need it, if it is on the dashboard, the users role already limit the search somehow, but this can be something to add,check this link, it tells when is_admin() should be use, in other words, only when we are in wp-admin, otherwise current_user_can('administrator') should be the function you need, maybe this has also something to do with it.