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.
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;
}
add_filter('posts_where','mam_posts_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
?>
smbrown
Member
Posted 6 months 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);