Dans la zone d'administration de la taxonomie, je filtre certains termes de taxonomie en fonction des méta-clés associées aux utilisateurs. La pagination pense toujours qu'il y a le nombre total de termes. Existe-t-il un crochet qui me permettrait de filtrer également la pagination? Je ne trouve rien en train de faire des recherches.
Ma meilleure théorie est de localiser l'endroit où la pagination est générée et de l'attacher à ce qu'elle utilise pour déterminer le nombre de pages. En parcourant le code source de WordPress, je ne vois rien qui me permettrait de le faire.
EDIT: Il s’agit d’une taxonomie personnalisée "preschool_class" sur un type de message personnalisé "preschool". J'ai collé la classe entière utilisée pour la fonctionnalité actuelle ci-dessous, modifiée de 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();
Cette ligne est ce qui filtre les termes sur la page de l'éditeur:
add_filter("list_terms_exclusions", array(&$this, "exclusions"));
Exclusions renvoie ceci:
" AND (t.term_id IN ($this->user_classes) OR tt.taxonomy NOT IN ('preschool_class'))"
Le problème racine, comme vous pouvez le voir ici , réside dans le fait que WP compte les éléments (termes) à paginer de cette façon:
'total_items' => wp_count_terms( $this->screen->taxonomy, compact( 'search' ) ),
Au lieu de cela, il devrait compter les termes à partir du résultat de la requête . Je crois que c'est un problème et qu'il devrait être signalé sur le traqueur de bogues WP .
Je confirme qu'il n'y a pas de crochets permettant de modifier la pagination.