I want to filter available terms for a user when they create/edit a post, I have assigned a custom field to the user where it returns an array of IDs for the categories they can edit.
In an action for load-post.php/load-post-new.php
I call my function, where I can loop through the terms for the category
taxonomy, and also retrieve my users available category IDs. I can then unset the term, which works when dumping the variable, however in the admin it appears the post category meta box is not affected by this, therefore I imagine I need to add a filter in order to get what I need. What would be the correct filter/hook to remove categories I do not need in the list?
Currently I am using:
add_action( 'load-post.php', array($this,'filter_user_categories'));
add_action( 'load-post-new.php', array($this,'filter_user_categories'));
Below is my functions inner code:
$current_user = get_current_user_id();
$user_field = "user_".$current_user;
$user_categories = get_field('post_categories', $user_field);
$terms = get_terms( array(
'taxonomy' => 'category',
'hide_empty' => false,
) );
$i=0;
foreach($terms as $term){
if(!in_array($term->term_id,$user_categories)){
echo $term->term_id;
unset($terms[$i]);
}
$i++;
}
I want to filter available terms for a user when they create/edit a post, I have assigned a custom field to the user where it returns an array of IDs for the categories they can edit.
In an action for load-post.php/load-post-new.php
I call my function, where I can loop through the terms for the category
taxonomy, and also retrieve my users available category IDs. I can then unset the term, which works when dumping the variable, however in the admin it appears the post category meta box is not affected by this, therefore I imagine I need to add a filter in order to get what I need. What would be the correct filter/hook to remove categories I do not need in the list?
Currently I am using:
add_action( 'load-post.php', array($this,'filter_user_categories'));
add_action( 'load-post-new.php', array($this,'filter_user_categories'));
Below is my functions inner code:
$current_user = get_current_user_id();
$user_field = "user_".$current_user;
$user_categories = get_field('post_categories', $user_field);
$terms = get_terms( array(
'taxonomy' => 'category',
'hide_empty' => false,
) );
$i=0;
foreach($terms as $term){
if(!in_array($term->term_id,$user_categories)){
echo $term->term_id;
unset($terms[$i]);
}
$i++;
}
Share
Improve this question
asked Jun 12, 2019 at 12:26
AravonaAravona
5851 gold badge5 silver badges19 bronze badges
1 Answer
Reset to default 0I managed to solve this issue using pre_get_terms
as an action and then updating the query_vars
$current_user = get_current_user_id();
$user_field = "user_".$current_user;
$user_categories = get_field('post_categories', $user_field);
if(in_array('category',$terms->query_vars['taxonomy'])){
$terms->query_vars['include'] = $user_categories;
}
return $terms;
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745389127a4625577.html
评论列表(0条)