Skip to content

Instantly share code, notes, and snippets.

@adnan360
Created July 6, 2017 06:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adnan360/333837a85bc7fc69a5914ac6941736d5 to your computer and use it in GitHub Desktop.
Save adnan360/333837a85bc7fc69a5914ac6941736d5 to your computer and use it in GitHub Desktop.
Wordpress/Woocommerce: Filter orders by custom meta tag
<?php
add_action( 'restrict_manage_posts', 'posts_filter_dropdown' );
function posts_filter_dropdown($post_type) {
// error_log('$post_type ====='.$post_type);
if('shop_order' != $post_type){
return;
}
$selected_option = @$_GET['order_type_filter'];
echo '
<select name="order_type_filter" id="mpms-order-type-filter">
<option value="">', __( 'Order Type', 'wordpress-seo' ), '</option>';
echo '<option value="takeout">Takeout</option>';
echo '<option value="delivery">Delivery</option>';
echo '
</select>';
}
add_filter( 'parse_query', 'filter_request_query' , 10);
function filter_request_query($query){
if( !(is_admin() AND $query->is_main_query()) ){
return $query;
}
if( !('shop_order' === $query->query['post_type']) && (isset($_REQUEST['order_type_filter']) ) ){
return $query;
}
if(0 == $_REQUEST['order_type_filter']){
return $query;
}
$query->query_vars = array(array(
'field' => '_store_order_type',
'value' => $_REQUEST['order_type_filter'],
'compare' => '=',
'type' => 'CHAR'
));
return $query;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment