categories - Correct HookFilter to amend category choices on post edit page

I want to filter available terms for a user when they createedit a post, I have assigned a custom field to the user whe

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
Add a comment  | 

1 Answer 1

Reset to default 0

I managed to solve this issue using pre_get_termsas 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

相关推荐

  • categories - Correct HookFilter to amend category choices on post edit page

    I want to filter available terms for a user when they createedit a post, I have assigned a custom field to the user whe

    2小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信