将视图添加到管理菜单以筛选特定条件(如果帖子是特定父项的子项)

时间:2018-10-31 作者:peter k

add_filter(\'views_edit-page\',\'addFilter\');

function addFilter($views) {

         global $wp_query;

         $query = array(
             \'post_type\'   => \'page\',
             \'post_status\' => \'publish\',
             \'post_parent\' => 2795,
         );

         $result = new WP_Query($query);


         $class = ($_GET[\'post_parent\'] == 2795) ? \' class="current"\' : \'\';
         $views[\'publish_f\'] = sprintf(__("<a href=\'%s\'". $class .">". \'Post Parent = 2795\' ." <span class=\'count\'>(%d)</span></a>", \'brookdale\' ), admin_url(\'edit.php?post_type=page&post_parent=2795\'), $result->found_posts);

         return $views;

}
我想为我的管理面板“页面”选项卡创建过滤器,该选项卡只显示父帖子的子页面、孙子页面或孙子页面。我当前显示了选项卡,但当我单击过滤器时,它不会将页面加载到word press页面列表中。我一直在努力寻找wordpress这个特定功能的文档,但在谷歌搜索中很难正确解释。

Shows the tab name, but the functionality does not work correctly

1 个回复
最合适的回答,由SO网友:Sally CJ 整理而成

您可以通过pre_get_posts 行动,如:

add_action( \'pre_get_posts\', \'my_admin_pre_get_posts\' );
function my_admin_pre_get_posts( $q ) {
  // Check whether the filter was clicked.
  if ( isset( $_GET[\'post_parent\'] ) ) {
    $screen = is_admin() ? get_current_screen() : null;

    // And that it\'s the edit pages page.
    if ( $screen && \'edit-page\' === $screen->id ) {
      $post_parent = (int) $_GET[\'post_parent\'];
      $q->set( \'post_parent\', $post_parent );
    }
  }
}

结束