# Filter Orders by Billing Country

> Adds a country dropdown to the admin orders list so you can see only orders from one country, handy for customs paperwork or regional reports.

- Type: PHP
- Runs: Admin only
- Category: [WooCommerce](https://wpide.com/library/category/woocommerce/)
- Requires: WooCommerce
- Tags: woocommerce, admin, orders, filters
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/woo-orders-filter-country/

## Code

```php
$wpide_country_dropdown = function () {
    $current = isset( $_GET['wpide_country'] ) ? strtoupper( sanitize_key( wp_unslash( $_GET['wpide_country'] ) ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification

    echo '<label for="wpide-country-filter" class="screen-reader-text">' . esc_html( 'Filter by country' ) . '</label>';
    echo '<select name="wpide_country" id="wpide-country-filter"><option value="">' . esc_html( 'All countries' ) . '</option>';

    foreach ( WC()->countries->get_allowed_countries() as $code => $name ) {
        printf( '<option value="%s" %s>%s</option>', esc_attr( $code ), selected( $current, $code, false ), esc_html( $name ) );
    }

    echo '</select>';
};

add_action( 'woocommerce_order_list_table_restrict_manage_orders', function ( $order_type = 'shop_order' ) use ( $wpide_country_dropdown ) {
    if ( 'shop_order' === $order_type ) {
        $wpide_country_dropdown();
    }
} );

add_action( 'restrict_manage_posts', function ( $post_type ) use ( $wpide_country_dropdown ) {
    if ( 'shop_order' === $post_type ) {
        $wpide_country_dropdown();
    }
} );

add_filter( 'woocommerce_order_list_table_prepare_items_query_args', function ( $args ) {
    if ( ! empty( $_GET['wpide_country'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
        $args['billing_country'] = strtoupper( sanitize_key( wp_unslash( $_GET['wpide_country'] ) ) ); // phpcs:ignore WordPress.Security.NonceVerification
    }

    return $args;
} );

add_action( 'pre_get_posts', function ( $query ) {
    if ( ! is_admin() || ! $query->is_main_query() || 'shop_order' !== $query->get( 'post_type' ) || empty( $_GET['wpide_country'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
        return;
    }

    $meta_query   = (array) $query->get( 'meta_query' );
    $meta_query[] = array( 'key' => '_billing_country', 'value' => strtoupper( sanitize_key( wp_unslash( $_GET['wpide_country'] ) ) ) ); // phpcs:ignore WordPress.Security.NonceVerification
    $query->set( 'meta_query', $meta_query );
} );
```

## Notes

Lists the countries you sell to and works with both HPOS and legacy order storage.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Filter Orders by Billing Country" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
