Skip to content

Instantly share code, notes, and snippets.

@Mahjouba91
Last active December 22, 2015 10:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mahjouba91/b9c25a72772e411cb528 to your computer and use it in GitHub Desktop.
Save Mahjouba91/b9c25a72772e411cb528 to your computer and use it in GitHub Desktop.
Wordpress Admin Filter : Filter top level pages
<?php
//defining the filter that will be used to select pages by 'top level pages'
add_action('restrict_manage_posts','add_post_formats_filter_to_post_administration');
function add_post_formats_filter_to_post_administration(){
//execute only on the 'post' content type
global $post_type;
if( $post_type == 'page' ){
$pages_arg = array(
'show_option_none' => 'Toutes les pages',
'depth' => 1,
'orderby' => 'NAME',
'order' => 'ASC',
'name' => 'page_top_pages_admin_filter',
);
//if we have a page already selected, ensure that its value is set to be selected
if( isset($_GET['page_top_pages_admin_filter']) ) {
$pages_arg['selected'] = sanitize_text_field($_GET['page_top_pages_admin_filter']);
}
wp_dropdown_pages($pages_arg);
}
}
// Update the WP Query to properly filter the admin page list
add_action('pre_get_posts','add_post_format_filter_to_posts');
function add_post_format_filter_to_posts($query){
global $post_type, $pagenow;
//if we are currently on the edit screen of the post type listings
if( $pagenow == 'edit.php' && $post_type == 'page' ) {
if( isset($_GET['page_top_pages_admin_filter']) ) {
//get the desired page
$page = sanitize_text_field($_GET['page_top_pages_admin_filter']);
if( $page != 0 ) {
$query->query_vars['page_id'] = $page;
}
}
}
}
@Mahjouba91
Copy link
Author

Goal : The goal of this gist is to only show top level pages in a wordpress website with a lot of subpages.

@Mahjouba91
Copy link
Author

TODO : Improve the dropdown to filter by depth level and not root pages directly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment