Skip to main content
4 of 4
deleted 581 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' && is_admin()){
        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;
    }
}