filter default wp_query to show just selected level of child pages in wordpress

Question

I’m working on a site that has a fairly large page structure a few levels deep – in some sections there are a lot of pages.

So I want to have functionality if user choose on select box level 1 then only pages in level 1 will show in below list of pages.
if he choose level 2 then only level 2 pages, same go to level 3 level 4 level 5 level 6.

it’s working for top level parent pages when I set query_vars[‘post_parent’] = 0; and I want to have same functionality to show list of level 1 child page,level 2 child pages and so on…

I am stuck on it. please I will be great full if anyone can help me for it. Thanks
see screenshot link
https://i.stack.imgur.com/EKpy6.png

function level_page_admin_posts_filter( $query ) {
  global $pagenow;
  if ( is_admin() && $pagenow == 'edit.php' && !empty($_GET['lavel_pages'])) {
      $level = $_GET['lavel_pages'];
      if($level == 'parent'){
        $query->query_vars['post_parent'] = 0;
      }else 
      if($level == 1){
        
      }else
      if($level == 2){

      }
  }
}
add_filter( 'parse_query', 'level_page_admin_posts_filter' );

function admin_page_filter_level_pages() {
  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="lavel_pages">
      <option value="parent">Parent Pages</option>
      <option value="1">Level 1</option>
      <option value="2">Level 2</option>
      <option value="3">Level 3</option>
      <option value="4">Level 4</option>
      ';
  
  $select .= ' </select>';
  echo $select;
} else {
  return;
}
}
add_action( 'restrict_manage_posts', 'admin_page_filter_level_pages' );
0
, , Hafiz Shehroz 1 year 2020-07-24T17:10:27-05:00 0 Answers 39 views 0

Leave an answer

Browse
Browse