Deleted pages showing up when querying for child pages

You’re using a raw SQL query to do a post query, this is bad, because:

  • There are already APIs that do this
  • It is no longer inheriting the default parameters
  • It doesn’t go through the caching system so it can be slower

Intead, use WP_Query.

The reason you’re getting trashed posts is because you’re not specifying what post status you want. By default WP_Query defaults to published only.

Here is your equivalent post loop:

$query = new WP_Query( array(
    'post_type' => 'page',
    'post_parent' => $post->ID,
    'posts_per_page' => -1,
    'orderby' => 'title',
    'order' => 'ASC'
) );

if ( $query->have_posts() ) {
    while( $query->have_posts() ) {
        $query->the_post();
        // process each post
    }
}

tech