代码之家  ›  专栏  ›  技术社区  ›  Tasos

从wordpress管理中的产品列表中删除特定的post状态

  •  1
  • Tasos  · 技术社区  · 8 年前

    在Woomerce中,我试图将所有产品隐藏在管理员中,并将草稿作为post状态。我所做的是:

    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');
    

    我删除了 all 和 mine 因为不管我是不是搬走了 draft ,这些产品在上述过滤器上仍然可见。

    现在,我的问题是当我打开页面时,默认视图仍然是 全部的 ,即使没有过滤器,所以 草案 产品可见。

    如何才能有效地从管理员列表中删除草稿产品?

    1 回复  |  直到 8 年前
        1
  •  1
  •   raju_eww    8 年前

    你的代码将隐藏 draft 只对所有状态高于产品表的产品执行此操作,因此必须使用该代码将其隐藏。

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

    并使用下面的代码隐藏 草案 表中的帖子

    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');