筛选器分类管理分页

时间:2017-08-16 作者:JacobTheDev

在分类管理区域,我根据与用户关联的元键筛选出一些分类术语。分页仍然认为有完整数量的术语。是否存在允许我过滤分页的挂钩?我找不到任何正在研究的东西。

我最好的理论是定位分页的生成位置,并连接到它用来确定页数的任何内容。查看WordPress源代码,我没有看到任何允许我这样做的内容。

编辑:这是针对自定义帖子类型“Prefore”上的自定义分类“Prefort\\u class”我已经粘贴了用于下面当前功能的整个类,该类是从this.

// limit teachers to their own classes
class FVPD_Teacher_Class_Restriction {
    private $user_classes = NULL;

    // activation hook
    public function add_teacher_capability_categories() {
        $role = get_role("teacher" );
        $role->add_cap("manage_categories");
    }

    // deactivation hook
    public function remove_teacher_capability_categories() {
        $role = get_role("teacher");
        $role->remove_cap("manage_categories");
    }

    // construct the class
    public function __construct() {
        // get the current page
        global $pagenow;

        // save the author ID for classes
        add_action("create_preschool_class", array(&$this, "save_class_author"));

        // set manage_categories capability for "teacher"
        add_action("admin_init", array(&$this, "add_teacher_capability_categories"));

        // remove manage_categories capability for "teacher"
        register_deactivation_hook(__FILE__, array(&$this, "remove_teacher_capability_categories"));

        // filter classes in new-post, edit-post, edit-tags
        add_action("admin_print_scripts-post-new.php", array(&$this, "filter_post_page"));
        add_action("admin_print_scripts-post.php", array(&$this, "filter_post_page"));
        add_action("admin_print_scripts-edit-tags.php", array(&$this, "filter_post_page"));

        // filter classes in the tag cloud
        add_filter("get_terms", array(&$this, "filter_tag_cloud"), 10, 4);

        // only show the current users posts
        if ($pagenow === "edit.php") {
            add_filter("pre_get_posts", array(&$this, "filter_edit_page"));
            add_action("current_screen", function ($current_screen) {
                if ($current_screen->id === "edit-preschool") {
                    add_filter("views_{$current_screen->id}", array(&$this, "list_table_views_filter"));
                }
            }, 20);
        } elseif ($pagenow === "edit-tags.php") {
            if (isset($_GET["taxonomy"]) && $_GET["taxonomy"] === "preschool_class") {
                // fix the pagination...
            }
        }
    }

    public function list_table_views_filter(array $views) {
        // return if the currently logged in user isn\'t a teacher
        if (!current_user_can("teacher")) {
            return $views;
        }

        // correct views so that they\'re associted to the currently logged in user
        foreach ($views as $view => $link) {
            if ($view === "all" && isset($views["mine"])) {
                unset($views[$view]);
            } elseif ($view === "mine") {
                $views[$view] = preg_replace("/Mine/", "All", $link);
            } elseif ($view === "publish") {
                $current_user_published_posts = get_posts(array(
                    "author"         => get_current_user_id(),
                    "fields"         => "ids",
                    "post_type"      => "preschool",
                    "post_status"    => "publish",
                    "posts_per_page" => -1,
                ));

                if (count($current_user_published_posts) > 0) {
                    $views[$view] = preg_replace("/(\\([0-9]+\\))/", "(" . count($current_user_published_posts) . ")", $link);
                } else {
                    unset($views[$view]);
                }
            } elseif ($view === "draft") {
                $current_user_draft_posts = get_posts(array(
                    "author"         => get_current_user_id(),
                    "fields"         => "ids",
                    "post_type"      => "preschool",
                    "post_status"    => "draft",
                    "posts_per_page" => -1,
                ));

                if (count($current_user_draft_posts) > 0) {
                    $views[$view] = preg_replace("/(\\([0-9]+\\))/", "(" . count($current_user_draft_posts) . ")", $link);
                } else {
                    unset($views[$view]);
                }
            } elseif ($view === "private") {
                $current_user_private_posts = get_posts(array(
                    "author"         => get_current_user_id(),
                    "fields"         => "ids",
                    "post_type"      => "preschool",
                    "post_status"    => "private",
                    "posts_per_page" => -1,
                ));

                if (count($current_user_private_posts) > 0) {
                    $views[$view] = preg_replace("/(\\([0-9]+\\))/", "(" . count($current_user_private_posts) . ")", $link);
                } else {
                    unset($views[$view]);
                }
            }
        }

        return $views;
    }

    // save "author" meta tag whenever a term is saved
    public function save_class_author($term_id) {
        add_term_meta($term_id, "author", get_current_user_id());
    }

    // get classes associated to the currently logged in user
    public function get_user_classes($user_id) {
        $terms = get_terms("preschool_class", array(
            "hide_empty" => false,
            "meta_query" => array(array(
                "key"   => "author",
                "value" => $user_id,
            )),
        ));

        $ids = "-1,";

        foreach ($terms as $term ) {
            $ids .= "{$term->term_id},";
        }

        $this->user_classes = substr($ids, 0, -1);
    }

    // SQL query to exclude any terms not associated to the currently logged in user
    public function exclusions($exclusions) {
        $exclusions .= " AND (t.term_id IN ($this->user_classes) OR tt.taxonomy NOT IN (\'preschool_class\'))";
        return $exclusions;
    }

    // filter out any terms not associated to the currently logged in user on the "new post" page
    public function filter_post_page() {
        // return if the currently logged in user isn\'t a teacher or the post type isn\'t preschool
        if (!current_user_can("teacher") || get_current_screen()->post_type !== "preschool") {
            return;
        }

        // get classes associated ot the currently logged in user
        $this->get_user_classes(get_current_user_id());

        // stop filtering if no classes are found
        if (empty($this->user_classes)) {
            return;
        }

        add_filter("list_terms_exclusions", array(&$this, "exclusions"));
    }

    // only display the classes associated to the currently logged in user
    public function filter_tag_cloud($terms, $taxonomies, $args, $term_query) {
        // ensure the currently logged in user is a teacher, is in the admin interface, and is requesting tags via AJAX
        if (current_user_can("teacher") && is_admin() && isset($_POST["action"]) && $_POST["action"] === "get-tagcloud") {
            $args["meta_query"] = array(array(
                "key"   => "author",
                "value" => get_current_user_id(),
            ));

            $terms = $term_query->query($args);

            return $terms;
        }

        return $terms;
    }

    // filter out any posts not associated to the currently logged in user on the "all posts" page
    public function filter_edit_page($query) {
        // return if the currently logged in user isn\'t a teacher
        if (!current_user_can("teacher")) {
            return;
        }

        $query->set("author", get_current_user_id());

        return $query;
    }
}

new FVPD_Teacher_Class_Restriction();
这一行用于过滤编辑器页面上的术语:

add_filter("list_terms_exclusions", array(&$this, "exclusions"));
排除返回:

" AND (t.term_id IN ($this->user_classes) OR tt.taxonomy NOT IN (\'preschool_class\'))"

1 个回复
SO网友:ClemC

正如你所看到的,根本问题here, 事实上,WP以这种方式计算分页的项目(术语):

\'total_items\' => wp_count_terms( $this->screen->taxonomy, compact( \'search\' ) ),
相反,它应该计算术语from the result of the query. 我认为这是一个问题,应该报告WP bug tracker.

我确认没有允许修改分页的挂钩。

结束

相关推荐

edit_{$taxonomy} | Hook

嗨,我对插件开发不是很有经验,我正在使用edit{$taxonomy}|钩子,但我无法使用这个钩子获得新的更新值。这是我的密码function action_edit_taxonomy( $term_id, $t_id ){ $term = get_term($term_id); print_r($term); exit; }; add_action( \"edit_um_user_tag\", \'action_edit_taxonomy\', 10, 6 );&