I have a custom post type called files. This post type is connected to a field group that includes:
-
3 taxonomies
-
1 checkbox list containing the available user roles on my site
I’m displaying the posts using an Elementor Loop Grid.
What I want to achieve:
-
First, filter the posts based on a comparison between the allowed roles (from the checkbox list) and the current user’s roles.
-
Then, apply Elementor’s native taxonomy filters dynamically on top of that.
What I have so far:
I wrote a small custom script (see below).
-
The user role filtering part works correctly: when I load the page, the posts displayed are restricted according to the current user’s roles.
-
However, the taxonomy filters do not work dynamically: when I click on a taxonomy filter, no posts appear. If I refresh the page, the filter is applied correctly, but I want it to work without needing a page reload.
Here’s my code:
<?php add_action("elementor/query/fichiers_query", function ($query) {
if (!is_user_logged_in()) {
$query->set("post__in", [0]);
return;
}
$user = wp_get_current_user();
$roles = (array) $user->roles;
if (empty($roles)) {
$query->set("post__in", [0]);
return;
}
$meta_queries = ["relation" => "OR"];
foreach ($roles as $role) {
$meta_queries[] = [
"key" => "roles",
"value" => '"' . $role . '"',
"compare" => "LIKE",
];
}
$query->set("meta_query", $meta_queries);
});
?>
Question:
How can I make Elementor’s taxonomy filters work dynamically after applying the initial user role filtering logic?
I hope you can help me on this,
thank you in advance!