[resolved] Exclude All Child Pages from WP_Query (4 posts)

  1. Drew Baker
    Posted 4 years ago #

    Hey Guys,
    I'm trying to figure out how to exclude all child pages from WP_Query?

    Here is what I am doing:

    <?php $my_query = new WP_Query("post_type=page&posts_per_page=9&orderby=menu_order&order=ASC"); ?>

    It's listing every page, but what I want is it to only show top-level pages and exclude all child pages. I was expecting the same functionality that wp_list_pages has with depth=1.

    Thanks for the help.

  2. vtxyzzy
    Posted 4 years ago #

    You should be able to do this with a filter:

    <?php function mam_posts_where ($where) {
       global $mam_global_where;
       if ($mam_global_where) $where .= " $mam_global_where";
       return $where;
    $mam_global_where = " AND $wpdb->posts.post_parent = 0 ";
    $my_query = new WP_Query("post_type=page&posts_per_page=9&orderby=menu_order&order=ASC");
    $mam_global_where = '';  // Turn off filter
  3. Drew Baker
    Posted 4 years ago #

    Totally worked, thanks!

  4. smbrown
    Posted 3 years ago #

    Without a filter:

    $args = array('post_type' => 'page',
    				'posts_per_page' => '9',
    				'post_parent' => '0',
    				'post_status' => 'publish' );
    $the_query = new WP_Query($args);

Topic Closed

This topic has been closed to new replies.

About this Topic