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.

Shows the tab name, but the functionality does not work correctly

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 );
    }
  }
}
  • If I wanted to include an additional conditional, one that also checks to see if the posts grand-parent is 2795 before displaying, how would I go about this? – peter k Nov 2 '18 at 17:46
  • You can use the post_parent__in parameter, which is a list (array) of post parent IDs. So if for example: great-grandparent = 1234, grandparent = 3456, parent = 5678; you can use $q->set( 'post_parent__in', array( 1234, 3456, 5678 ) ); to query for posts whose post_parent is one of the specified post IDs. Let me know if you need a sample code. – Sally CJ Nov 2 '18 at 20:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.