WP +WPML 在 edit.php 中仅显示属于已登录用户的帖子并更新过滤器计数

问题描述 投票:0回答:1

我正在尝试过滤帖子以仅显示属于已登录用户的帖子。我正在使用 WMPL 翻译插件。问题是,如果我过滤掉帖子,我仍然会在页面顶部的 edit.php 页面上看到所有帖子和所有语言的过滤器计数。

Filter count example img

在上面的 img 中,我根据我的数据库写了实数应该是多少。只有我的 (9) 是正确的——我有 9 个帖子用所有语言计算。

在表格中一切正常,它只显示登录的用户帖子,即使我选择 All(12) 过滤器我也只看到 9 个帖子。那么我怎样才能更新过滤器以正确计数呢?

我在我的子主题 functions.php 中使用这段代码来过滤帖子:

function posts_for_current_author($query) {
  global $pagenow;

  if( 'edit.php' != $pagenow || !$query->is_admin ){
    return $query;
  }
      

  if( !current_user_can( 'manage_options' ) ) {
      global $user_ID;
      $query->set('author', $user_ID );
  }
  return $query;
}
add_filter('pre_get_posts', 'posts_for_current_author');
wordpress wpml
1个回答
0
投票

好的,希望这对将来的人有帮助,这是我想出的解决方案:

首先,我取消设置显示所有用户的所有帖子的所有过滤器,我只剩下我自己的帖子视图。

代码转到子主题 functions.php

 /*remove All and Published counts from edit.php list of posts */
add_filter('views_edit-objektai', 'custom_editor_counts');
add_filter('views_edit-marsrutai', 'custom_editor_counts');


function get_user_role() {
  global $current_user;

  $user_roles = $current_user->roles; 
  $user_role = array_shift($user_roles); 

  return $user_role;
}


function custom_editor_counts($views) {
  $user_role = get_user_role();
  if($user_role === 'author'){
    unset($views['all']);
    unset($views['publish']);
  }
  return $views;
}

然后我在寻找一个 WPML 挂钩来编辑那些语言过滤器计数器,但我没有找到一个挂钩。所以我直接编辑了插件。所以这些更改只会对我有帮助,直到我更新插件......

负责此功能的函数位于:\wp-content\plugins\sitepress-multilingual-cms\menu\post-menus\wpml-post-language-filter.class.php

函数名是get_count_data();

我改成这样:

protected function get_count_data( $type ) {
    $extra_conditions = $this->extra_conditions_snippet();
    $user_id = get_current_user_id();
    
    global $current_user;
    $user_roles = $current_user->roles; 
    $user_role = array_shift($user_roles);

      if($user_role === 'author'){
        return $this->wpdb->get_results( $this->wpdb->prepare("
            SELECT language_code, COUNT(p.ID) AS c
            FROM {$this->wpdb->prefix}icl_translations t
            JOIN {$this->wpdb->posts} p
                ON t.element_id=p.ID
                    AND t.element_type = CONCAT('post_', p.post_type)
            WHERE p.post_type=%s AND p.post_author = %d {$extra_conditions}
            ", $type, $user_id ) ); // added extra filter in this query
      }
    

    return $this->wpdb->get_results( $this->wpdb->prepare("
            SELECT language_code, COUNT(p.ID) AS c
            FROM {$this->wpdb->prefix}icl_translations t
            JOIN {$this->wpdb->posts} p
                ON t.element_id=p.ID
                    AND t.element_type = CONCAT('post_', p.post_type)
            WHERE p.post_type=%s {$extra_conditions}
            ", $type ) ); // left the default query for all the rest user roles
} 

正如我所说,这将在插件更新之前有效,所以如果有人知道更好的解决方案,请随时分享。

© www.soinside.com 2019 - 2024. All rights reserved.