1

I have two types of admin that can view the orders list. one of the admins just can view some of the orders.

I use below code to manipulate orders query for that admin:

add_action('pre_get_posts', function ($query) {
    global $pagenow;
    if (isset($query->query_vars['post_type'])) {
        if ($query->query_vars['post_type'] == 'shop_order' && ('edit.php' == $pagenow)) {

            if (!$query->is_main_query()) {
                return;
            }
            // I manipulate $query here
        }
    }
}, 1000, 1);

this code shows the orders that I want to show to that admin but this doesn't affect the order statuses on top of the table. I mean these numbers:

enter image description here

how can I fix these numbers to show correct order statuses to that admin?

1 Answer 1

2

Normally pre_get_post doesn't change wordpress views post count. You have to call another filter hook to change to count.

add_action('pre_get_posts', function ($query) {
global $pagenow;
if (isset($query->query_vars['post_type'])) {
    if ($query->query_vars['post_type'] == 'shop_order' && ('edit.php' == $pagenow)) {

        if (!$query->is_main_query()) {
            return;
        }
        // I manipulate $query here

        // Get Post Count Here For Each Status

        add_filter( 'views_edit-shop_order', function( $views ) {

            $views['all'] = sprintf("<a href='%s'>All (%d)", $url_to_redirect, $all_count );
            $views['wc-processing'] = sprintf("<a href='%s'>Processing (%d)", $url_to_redirect, $processing_count );
            $views['wc-completed'] = sprintf("<a href='%s'>Completed (%d)", $url_to_redirect, $completed_count );
            $views['wc-failed'] = sprintf("<a href='%s'>Failed (%d)", $url_to_redirect, $failed_count );

            return $views;

            // Sample array we get print_r( $views )
            Array
            (
                [all] => All (24)
                [wc-processing] => Processing (3)
                [wc-completed] => Completed (19)
                [wc-failed] => Failed (2)
            )
        });
    }
}
}, 1000, 1);
1
  • what is $url_to_redirect? could you please explain to me? Dec 14, 2019 at 7:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.