Add view to admin menu to filter for specific criteria ( If post is child of specific Parent )

admin2025-06-06  0

add_filter('views_edit-page','addFilter');

function addFilter($views) {

         global $wp_query;

         $query = array(
             'post_type'   => 'page',
             'post_status' => 'publish',
             'post_parent' => 2795,
         );

         $result = new WP_Query($query);


         $class = ($_GET['post_parent'] == 2795) ? ' class="current"' : '';
         $views['publish_f'] = sprintf(__("<a href='%s'". $class .">". 'Post Parent = 2795' ." <span class='count'>(%d)</span></a>", 'brookdale' ), admin_url('edit.php?post_type=page&post_parent=2795'), $result->found_posts);

         return $views;

}

I want to create filter for my Admin panel "Pages" tab that only shows pages that are children or grand-children or great-grand-children of the parent post. I currently have the tab showing, but when I click the filter it does not load the pages into the word press pages list. I have been trying to find documentation on this specific function for wordpress but it's a bit difficult to explain correctly in a google search.

add_filter('views_edit-page','addFilter');

function addFilter($views) {

         global $wp_query;

         $query = array(
             'post_type'   => 'page',
             'post_status' => 'publish',
             'post_parent' => 2795,
         );

         $result = new WP_Query($query);


         $class = ($_GET['post_parent'] == 2795) ? ' class="current"' : '';
         $views['publish_f'] = sprintf(__("<a href='%s'". $class .">". 'Post Parent = 2795' ." <span class='count'>(%d)</span></a>", 'brookdale' ), admin_url('edit.php?post_type=page&post_parent=2795'), $result->found_posts);

         return $views;

}

I want to create filter for my Admin panel "Pages" tab that only shows pages that are children or grand-children or great-grand-children of the parent post. I currently have the tab showing, but when I click the filter it does not load the pages into the word press pages list. I have been trying to find documentation on this specific function for wordpress but it's a bit difficult to explain correctly in a google search.

Share Improve this question asked Oct 31, 2018 at 17:30 peter kpeter k 54 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

You can "load the pages" via the pre_get_posts action, like so:

add_action( 'pre_get_posts', 'my_admin_pre_get_posts' );
function my_admin_pre_get_posts( $q ) {
  // Check whether the filter was clicked.
  if ( isset( $_GET['post_parent'] ) ) {
    $screen = is_admin() ? get_current_screen() : null;

    // And that it's the edit pages page.
    if ( $screen && 'edit-page' === $screen->id ) {
      $post_parent = (int) $_GET['post_parent'];
      $q->set( 'post_parent', $post_parent );
    }
  }
}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749212829a317308.html

最新回复(0)