Parent pages filter function your WordPress admin section

If your WordPress Blog has a lot of pages (not posts) it becomes very hard to select them in the  pages list (WordPress back-end). A select menu with all the parent (top) pages would be a great and useful filter function (like the categories filter in the posts section).

Check this comment for a modified function which is able to filter grouped products in WooCommerce.

The function below will add the select menu with the parent pages as a filter:

function fws_admin_posts_filter( $query ) {
    global $pagenow;
    if ( is_admin() && $pagenow == 'edit.php' && !empty($_GET['my_parent_pages'])) {
        $query->query_vars['post_parent'] = $_GET['my_parent_pages'];
    }
}
add_filter( 'parse_query', 'fws_admin_posts_filter' );

function admin_page_filter_parentpages() {
    global $wpdb;
    if (isset($_GET['post_type']) && $_GET['post_type'] == 'page') {
		$sql = "SELECT ID, post_title FROM ".$wpdb->posts." WHERE post_type = 'page' AND post_parent = 0 AND post_status = 'publish' ORDER BY post_title";
		$parent_pages = $wpdb->get_results($sql, OBJECT_K);
		$select = '
			<select name="my_parent_pages">
				<option value="">Parent Pages</option>';
		$current = isset($_GET['my_parent_pages']) ? $_GET['my_parent_pages'] : '';
		foreach ($parent_pages as $page) {
			$select .= sprintf('
				<option value="%s"%s>%s</option>', $page->ID, $page->ID == $current ? ' selected="selected"' : '', $page->post_title);
		}
		$select .= '
			</select>';
		echo $select;
	} else {
		return;
	}
}
add_action( 'restrict_manage_posts', 'admin_page_filter_parentpages' );

How-to use this PHP code

Open the file named “functions.php” which is inside your themes directory and copy the code into this file and re-visit the pages section.

This function as based on some code I found at WP-Snippets.

Tip! If you need more or better filter options, try Admin Columns. You need to buy the PRO version to use all features, but it’s worth any penny!

15 thoughts on “Parent pages filter function your WordPress admin section”

  1. Very helpful, thank you! Any tips for displaying children and grandchildren of the selected page?

  2. hi i realy need this code to work with woocommerce for grouped products, when i select a grouped pruduct it shows products that belongs to the group

    is it possible?

  3. Hi Ricardo,
    I see that a grouped product in WooCommerce has also a child parent relation. Just like pages, so it should be possible!

    So the first you need to do is change the post_type in this function from “page” to “product”:

    https://gist.github.com/finalwebsites/8076b52e9f7bea2cfe62bd565a07d640

    Since there are so many products where the post_parent value = 0, it was a bit tricky to keep them out of the select menu.
    But, I think this is the result you’re looking for, add this code into your theme’s functions.php file.

    screenshot

  4. yes!

    I never thought when I wrote here get an answer, because I saw that was published in 2011.

    thank you very much

    1. Hi Ricardo,
      No problem, I’m glad that this code snippet still works and with a small modification even for grouped products in WooCommerce.

  5. Thanks! I simply pasted your code into my child theme’s functions.php, and it still works great in WordPress 4.1.1.

    1. Yep, this is how it works :) I’m glad you like it.

  6. hi, thanks for the code, very useful.
    I suggest that you can use the `get_pages` function instead of a query (lines 12-13):

    `
    $parent_pages = get_pages( array(‘parent’ => 0) );
    `

    1. Ooh yes, you’re right there is no need to have this query. I will change it the next time that I can try the modification first (too busy right now :)) Actually I can use `get_pages` also for WooCommerce parent / child filter.

  7. Great! But it only shows pages which are direct siblings. Next level pages are not shown :-(

    1. Yes that’s right, the function shows only top level pages and filters than the direct siblings.
      This should work for many situations… BTW don’t create to many pages! They might slow down your website. Use custom post types instead.

  8. WordPress 4.7, works great! Fills my needs now, but I will need something that can show me full depth filters, or filter by any parent, rather than only site root level parents. Any chance that might be made available anytime soon? Thanks.

    1. Hi,
      actually no, there are no plans, because I think that having so complex page structures is a bad concept for WordPress. I might even become a usability issue if you create to much pages. Split different sections up into custom post types (services, cases, etc.) and use categories to filter or group them. You can use parent-child relations for custom taxonomies too.

  9. hello, i have a question, how to add grouping in the post, not pages, i mean in order to make parent post and a couple of child post in the same categories?.

    thank you.

    1. Hi, actually this is not possible for regular posts.
      A (custom) post type need to have the “hierarchical” attribute, otherwise the parent/child relations are not possible.
      If you need this, create a custom post type with this function (or a plugin, I use https://wordpress.org/plugins/custom-post-type-ui/) https://developer.wordpress.org/reference/functions/register_post_type/ and set the attribute “hierarchical” to true.
      If you have such a post type, you can add categories or custom taxonomies too.

Leave a Reply to Olaf Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.