Skip to content

Instantly share code, notes, and snippets.

@SirDarcanos
Created October 30, 2023 04:48
Show Gist options
  • Save SirDarcanos/2edd2ff4b9e763c0a45e6c251d866f0d to your computer and use it in GitHub Desktop.
Save SirDarcanos/2edd2ff4b9e763c0a45e6c251d866f0d to your computer and use it in GitHub Desktop.
<?php
// Predefined email address
define('HIDDEN_EMAIL_ORDERS', '[email protected]');
// Add the checkbox
function add_email_filter_checkbox() {
global $typenow;
if ( 'shop_order' == $typenow ) {
$is_checked = isset( $_GET['hide_specific_email'] ) ? 'checked' : '';
echo '<label style="margin-right:10px;margin-left:10px;"><input style="height:1rem;" type="checkbox" name="hide_specific_email" value="1" ' . $is_checked . '> Hide orders from ' . HIDDEN_EMAIL_ORDERS . '</label>';
}
}
add_action( 'restrict_manage_posts', 'add_email_filter_checkbox' );
// Filter orders based on the checkbox value
function filter_orders_by_checkbox( $query ) {
global $typenow, $wpdb;
if ( 'shop_order' == $typenow && isset( $_GET['hide_specific_email'] ) && '1' == $_GET['hide_specific_email'] ) {
$order_ids = $wpdb->get_col( $wpdb->prepare(
"SELECT pm.post_id FROM {$wpdb->postmeta} pm WHERE pm.meta_key = '_billing_email' AND pm.meta_value = %s",
HIDDEN_EMAIL_ORDERS
) );
// Adjust the main query to exclude orders with the specified email
$query->set( 'post__not_in', $order_ids );
}
}
add_action( 'pre_get_posts', 'filter_orders_by_checkbox' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment