使用add_filter - 基于类别和角色

时间:2014-01-08 09:39:18

标签: php wordpress-theming wordpress

我可以成功使用'add_filter'在发布帖子时将帖子状态更改为“待处理”。

function publish_as_pending( $data , $postarr ) {  

  $data['post_status'] = 'pending';   

  return $data;

}

add_filter('wp_insert_post_data' , 'publish_as_pending' , '99', 2); 

如何将此更改为仅在过滤时进行过滤;

a)职位类别ID为1

b)用户发布是角色'贡献者'。

1 个答案:

答案 0 :(得分:1)

Add This Function on Function.php
function filter_handler( $data , $postarr ){
    if($postarr['post_category']):
        foreach( $postarr['post_category'] as $category_id ) {
             if ($category_id ==1 || current_user_can('contributor')){
                 $data['post_status'] = 'pending';   
                 return $data;
             }
        }
    endif;    
    return $data;

}
add_filter( 'wp_insert_post_data', 'filter_handler', '99', 2 );

moreinfo to Go.