最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin10PV0评论
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 );
    }
  }
}
Post a comment

comment list (0)

  1. No comments so far