Last active
November 11, 2018 18:55
-
-
Save certainlyakey/f028d4c64fd28c61f16e57ea575f78c3 to your computer and use it in GitHub Desktop.
Hide certain pages (ones with system-pages category assigned) from the general admin listing (Wordpress)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // 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 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', 'hide_system_pages' ); | |
| // Add system pages to quick links in the admin listing | |
| function 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', 'add_system_pages_to_quick_links'); | |
| function register_taxonomies() { | |
| register_taxonomy_for_object_type( 'category', 'page' ); | |
| } | |
| add_action( 'init', 'register_taxonomies' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment