1

I'm using the following code to display the featured image, post_title and custom field data from relevant child pages.

<?
$child_pages = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_parent = ".$post->ID." AND post_type = 'page' ORDER BY 'title ASC' ", 'OBJECT');

if ( $child_pages ) :
    foreach ( $child_pages as $pageChild ) :
        setup_postdata( $pageChild );
        $thumbnail = get_the_post_thumbnail($pageChild->ID, 'thumbnail');
        if($thumbnail == "") continue; // Skip pages without a thumbnail
?>
        <div class="child-thumb">
          <a href="<?= get_permalink($pageChild->ID) ?>" rel="bookmark" title="<?= $pageChild->post_title ?>">
            <?= $thumbnail ?><br />
           <?php echo get_post_meta($pageChild->ID, 'textfield', true); ?> </a><br /> <! -- Add riding-->
            <?= $pageChild->post_title ?>

        </div>
    <?
        endforeach;
    endif;
    ?>

However, it's also displaying child pages that have been deleted. How can I prevent it from checking the trash and displayed deleted pages?

Thank you!

1
  • 2
    Do not use wpdb to grab posts, get_posts or WP_Query should be used
    – Tom J Nowell
    Jun 25 '14 at 15:29
1

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
    }
}
3

Unless there's a very good (usually performance, or, of course, inability of core functions to do what you need) reason to use raw SQL requests to fetch posts and other data in WordPress, you shouldn't construct your own SQL queries. Instead, you can use WP_Query. In your case, you could fetch the posts like this:

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

$posts = $query->posts;

The problem with your SQL query is that it's not considering the post status (trash for trashed posts). WP_Query automatically excludes trashed posts (and revisions, and more) but is above all a layer of abstraction, and has many more advantages like being filterable.

0

Use get_posts() or WP_Query for secondary queries. They will use the post status 'publish' by default.

If you still want to use a custom query add the post status to it

$child_pages = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_status = 'publish' AND post_parent = ".$post->ID." AND post_type = 'page' ORDER BY 'title ASC' ", 'OBJECT');

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.