Last active
October 30, 2017 08:27
-
-
Save certainlyakey/7d7a374eab29f9bc824e824ffd3f933c to your computer and use it in GitHub Desktop.
Hide certain pages from WP admin (based on an ACF/custom field)
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
| acf_add_local_field_group(array ( | |
| 'key' => 'group_ujq7wrdtwyldi', | |
| 'title' => __('System settings','domain'), | |
| 'fields' => array ( | |
| array ( | |
| 'key' => 'field_sf9o6xmozloau', | |
| 'label' => __('Hide this page for non admins?','domain'), | |
| 'name' => 'page_is_system', | |
| 'type' => 'true_false', | |
| 'instructions' => '', | |
| 'required' => 0, | |
| 'conditional_logic' => 0, | |
| 'wrapper' => array ( | |
| 'width' => '', | |
| 'class' => '', | |
| 'id' => '', | |
| ), | |
| 'message' => '', | |
| 'default_value' => 0, | |
| 'ui' => 1, | |
| 'ui_on_text' => '', | |
| 'ui_off_text' => '', | |
| ), | |
| ), | |
| 'location' => array ( | |
| array ( | |
| array ( | |
| 'param' => 'post_type', | |
| 'operator' => '==', | |
| 'value' => 'page', | |
| ), | |
| array ( | |
| 'param' => 'user_role', | |
| 'operator' => '==', | |
| 'value' => 'administrator', | |
| ), | |
| ), | |
| ), | |
| 'menu_order' => 0, | |
| 'position' => 'normal', | |
| 'style' => 'default', | |
| 'label_placement' => 'top', | |
| 'instruction_placement' => 'label', | |
| 'hide_on_screen' => '', | |
| 'active' => 1, | |
| 'description' => '', | |
| )); |
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
| // Hide certain pages (ones with a field assigned) from the general admin listing | |
| function theme_hide_meta_system_pages($query) { | |
| if ( | |
| is_admin() && | |
| !empty( $_GET['post_type'] ) && | |
| $_GET['post_type'] == 'page' && | |
| $query->query['post_type'] == 'page' | |
| ) { | |
| if (!isset($_GET['page_is_system']) || $_GET['page_is_system'] == '0') { | |
| $query->set('meta_query', array( | |
| 'relation' => 'OR', | |
| // set to 0 | |
| array( | |
| 'key' => 'page_is_system', | |
| 'value' => '1', | |
| 'compare' => '!=' | |
| ), | |
| // does not exist | |
| array( | |
| 'key' => 'page_is_system', | |
| 'compare' => 'NOT EXISTS', | |
| 'value' => '' | |
| ) | |
| )); | |
| } else { | |
| $query->set('meta_query', array( | |
| // set to 1 | |
| array( | |
| 'key' => 'page_is_system', | |
| 'value' => '1', | |
| 'compare' => '=' | |
| ), | |
| )); | |
| } | |
| } | |
| } | |
| add_action('pre_get_posts', 'theme_hide_meta_system_pages'); | |
| function theme_add_meta_system_pages_queryvar($vars) { | |
| $vars[] = 'page_is_system'; | |
| return $vars; | |
| } | |
| add_filter( 'query_vars', 'theme_add_meta_system_pages_queryvar' ); | |
| // Add system pages to quick links in the admin listing | |
| function theme_add_meta_system_pages_to_quick_links($views) { | |
| if ((is_admin()) && ($_GET['post_type'] == 'page')) { | |
| global $wp_query; | |
| $class = (isset($wp_query->query_vars['page_is_system']) && $wp_query->query_vars['page_is_system'] == '1') ? ' class="current"' : ''; | |
| $views['system'] = '<a href="' . admin_url('edit.php?post_type=page&page_is_system=1') . '" ' . $class . '>' . __('System pages','domain') . '</a>'; | |
| return $views; | |
| } | |
| } | |
| add_filter('views_edit-page', 'theme_add_meta_system_pages_to_quick_links'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment