Skip to content

Instantly share code, notes, and snippets.

@temsool
Created January 12, 2023 15:35
Show Gist options
  • Save temsool/26948bcc8af3afd9dd29bab31759073f to your computer and use it in GitHub Desktop.
Save temsool/26948bcc8af3afd9dd29bab31759073f to your computer and use it in GitHub Desktop.
Parent pages filter function your WordPress admin section https://www.tutdepot.com/add-a-parent-pages-filter-function-your-wordpress-admin-section/
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' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment