Tasos
Tasos

Reputation: 7587

Remove specific post status from products list in Wordpress Admin

In a Woocommerce, I try to hide all products in Admin with Draft as post status. What I have done is:

function remove_draft_all_mine_products($views) {

  unset($views['draft']);
  unset($views['all']);
  unset($views['mine']);
  return $views;
}
add_filter('views_edit-product', 'remove_draft_all_mine_products');

I removed all and mine as well since regardless if I remove draft, those products are still visible on the above filters.

Now, my problem is that when I open the page, the default view is still with all, even without the filter, so draft products are visible.

How can I effectively remove draft products only from the admin list?

Upvotes: 2

Views: 2840

Answers (1)

raju_odi
raju_odi

Reputation: 1475

your code will hide draft only from all product with status above product table so you have to use that code to hide it.

function remove_draft_all_mine_products($views) {

  unset($views['draft']);
  return $views;
}
add_filter('views_edit-product', 'remove_draft_all_mine_products');

and use below code to hide draft posts from table

function wpse_288586_hide_products($query) {
    // We have to check if we are in admin and check if current query is the main query and check if you are looking for product post type
    if(is_admin() && $query->is_main_query() && $query->get('post_type') == "product") {
        $query->set('post_status',array('publish', 'pending', 'future', 'private', 'inherit', 'trash'));
    }
}
add_action('pre_get_posts', 'wpse_288586_hide_products');

Upvotes: 3

Related Questions