certainlyakey
10/26/2017 - 6:44 AM

Hide certain pages (ones with system-pages category assigned) from the general admin listing

Hide certain pages (ones with system-pages category assigned) from the general admin listing (Wordpress)

// Hide certain pages (ones with system-page category assigned) from the general admin listing, but keep them accessible when visiting category listing in admin
function stumppi_hide_system_pages($query) {
  if (
    is_admin() && 
    !empty( $_GET['post_type'] ) && 
    $_GET['post_type'] == 'page' && 
    !$_GET['category_name'] && 
    $query->query['post_type'] == 'page'
  ) {
    $query->set( 'tax_query', array(array(
      'taxonomy' => 'category',
      'field' => 'slug',
      'terms' => array('system-pages'),
      'operator' => 'NOT IN'
    )));
   }
}

add_action( 'pre_get_posts', 'stumppi_hide_system_pages' );


// Add system pages to quick links in the admin listing
function stumppi_add_system_pages_to_quick_links($views) {

  if ((is_admin()) && ($_GET['post_type'] == 'page')) {

    global $wp_query;
    $category_slug = 'system-pages';

    $system_pages_query = array(
      'post_type'     => 'page',
      'category_name' => $category_slug
    );

    $cat_object = get_category_by_slug($category_slug);

    $class = ($wp_query->query_vars['category_name'] == $category_slug) ? ' class="current"' : '';
    $views['system'] = sprintf(__('<a href="%s"'. $class .'>'. $cat_object->name .'</a>', 'theme_domain' ), admin_url('edit.php?post_type=page&category_name=' . $category_slug));

    return $views;

  }
}

add_filter('views_edit-page', 'stumppi_add_system_pages_to_quick_links');