0

How can I filter out post lists in WP's admin so that only child and grandchild posts are visible?

So that a list like

  • Item 1
    • Item 1.1
      • Item 1.1.1
      • Item 1.1.2
    • Item 1.2
      • Item 1.2.1
      • Item 1.2.1
      • Item 1.2.3
    • Item 1.3

Would become

  • Item 1.1
      • Item 1.1.1
      • Item 1.1.2
  • Item 1.2
    • Item 1.2.1
      • Item 1.2.1
      • Item 1.2.3
    • Item 1.3

I've tried using the parse_query -filter in the admin side but I can only get the children without the grand childs, or the grandchilds but under the wrong child.

1 Answer 1

0

Without seeing your code and arguments you are using in parse_query filter, I can be certain, but if think the post_parent__not_in query argument achieves what you want.

function wpse_282340_exclude_posts_from_admin($query)
{
    // bail early if we are not in the admin side
    if (!is_admin()) {
        return $query;
    }

    global $pagenow, $post_type;

    // check if we are in the edit screen for page post type
    if ($pagenow == 'edit.php' && $post_type == 'page') {
        // filter out the pages with post_parent = 0 (ie. no post_parent, first level pages)
        $query->query_vars['post_parent__not_in'] = array(0);
    }

    return $query;

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

Let me know if works for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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