Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // filter the posts clauses for some shenanigans
- // order by an option meta field and then by post date
- add_filter( 'posts_clauses', 'kia_edit_posts_clauses', 10, 2 );
- function kia_edit_posts_clauses( $pieces, $query ) {
- if ( $query->get( 'orderby' ) == 'dealer_date' && isset( $query->query_vars['query_id'] ) && $query->query_vars['query_id'] == "woocommerce_my_account_my_orders_query" ) {
- global $wpdb;
- // join the post_meta table
- $pieces[ 'join' ] .= $wpdb->prepare( ' LEFT JOIN ' . $wpdb->postmeta . ' dealer_pm ON dealer_pm.post_id = ' . $wpdb->posts . '.ID AND dealer_pm.meta_key = %s AND LENGTH(TRIM(dealer_pm.meta_value) )', '_shipping_dealer_number' );
- // Negate the meta_value if it exists to sort before zero.
- $pieces[ 'fields' ] .= ', CASE WHEN dealer_pm.meta_value IS NOT NULL THEN -dealer_pm.meta_value ELSE 0 END AS dealer_number';
- $pieces[ 'orderby' ] = $pieces[ 'orderby' ] = 'ISNULL(MAX(dealer_pm.meta_value)) OR LENGTH(TRIM(MAX(dealer_pm.meta_value))) = 0 ASC, MAX(dealer_pm.meta_value) ASC,' . $wpdb->posts . '.post_date DESC';
- }
- return $pieces;
- }
- // then my query
- $customer_orders = get_posts( apply_filters( 'woocommerce_my_account_my_orders_query', array(
- 'query_id' => 'woocommerce_my_account_my_orders_query',
- 'posts_per_page' => $order_count,
- 'no_found_rows' => true,
- 'date_query' => array(
- array(
- 'after' => '2 months ago',
- ),
- ),
- 'meta_query' => array(
- array(
- 'key' => '_customer_user',
- 'value' => get_current_user_id(),
- 'compare' => '=',
- ),
- ),
- 'post_type' => wc_get_order_types( 'view-orders' ),
- 'post_status' => array_keys( wc_get_order_statuses() ),
- 'suppress_filters' => false,
- 'orderby' => 'dealer_date'
- ) ) );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement