exclude - Create a list of pages excluding children of selected page

admin2025-06-04  43

I am looking to find a way to create a list of the IDs of children pages from a selected page.

My goal is to use the list of IDs so that I can exclude them from the following function:

wp_list_pages(array('exclude' => $###Children_Pages_to_be_excluded###, 'title_li' => ''));

I have checked the following question, but I am not sure how to use this function to achieve what I am trying to do.| Trying to list out child pages with WP_Query

I would appreciate any pointers. Many thanks for your assistance and time.

P.S.

I have managed to solve the problem with the following:

//Create an array containing the IDs of pages that are children of page ID XXX"
$args = array(
'post_type' => 'page',
'post_parent' => XXX,
'fields' => 'ids',
'posts_per_page' => -1,
);
$qry = new WP_Query($args);

//Convert the array into a string
$string = implode(', ', $qry->posts);

//Create a list of all pages
wp_list_pages(array('exclude' => $string, 'title_li' => ''));

I am looking to find a way to create a list of the IDs of children pages from a selected page.

My goal is to use the list of IDs so that I can exclude them from the following function:

wp_list_pages(array('exclude' => $###Children_Pages_to_be_excluded###, 'title_li' => ''));

I have checked the following question, but I am not sure how to use this function to achieve what I am trying to do.| Trying to list out child pages with WP_Query

I would appreciate any pointers. Many thanks for your assistance and time.

P.S.

I have managed to solve the problem with the following:

//Create an array containing the IDs of pages that are children of page ID XXX"
$args = array(
'post_type' => 'page',
'post_parent' => XXX,
'fields' => 'ids',
'posts_per_page' => -1,
);
$qry = new WP_Query($args);

//Convert the array into a string
$string = implode(', ', $qry->posts);

//Create a list of all pages
wp_list_pages(array('exclude' => $string, 'title_li' => ''));
Share Improve this question edited Jan 7, 2019 at 15:43 Peter asked Jan 7, 2019 at 14:05 PeterPeter 12 bronze badges 8
  • Keep in mind that any kind of "exclude" or "not_in" type queries can be surprisingly expensive/slow, and don't scale very far. Why are you trying to exclude them? If you provide more context there may be an alternative that doesn't involve wp_list_pages or the exclude but still gives you the same result – Tom J Nowell Commented Jan 7, 2019 at 14:19
  • Thanks for your reply! Here is what I am trying to do in more detail. I am creating a dynamic HTML sitemap page containing all the pages from my website. For this purpose I am using the following function: wp_list_pages(array('exclude' => '', 'title_li' => '')); This query works fine and it produces exactly the result I am looking for with one exception. – Peter Commented Jan 7, 2019 at 14:27
  • What I am trying to do next is to exclude a list of pages, that are all children of a selected page X. Here is where I'm at currently: $args = array( 'child_of' => ###ID_of_parent_page_X 'post_type' => 'page', 'post_status' => 'publish' ); $excluded_pages = get_pages($args); The final query produces an array of the CONTENT of pages. However, I need an Array of the page IDs, not their content. Finally, I am trying to use the $excluded_pages variable as an exclude term inside the first function (above). Hope this explanation is sufficient. Thank you in advance! – Peter Commented Jan 7, 2019 at 14:28
  • P.S. I have managed to create a list of IDs of the child pages using the following query: $args = array( 'post_type' => 'page', 'post_parent' => 11702, 'fields' => 'ids', 'posts_per_page' => -1, ); $qry = new WP_Query($args); var_dump($qry->posts); I am trying to use it as exclude term here: wp_list_pages(array('exclude' => '$qry->posts', 'title_li' => '')); Apparently, I am missing something, as the pages that need to be excluded are still present in the sitemap. – Peter Commented Jan 7, 2019 at 15:03
  • Can you edit your question to include that code inside code blocks? It's unreadable in comments and difficult to follow – Tom J Nowell Commented Jan 7, 2019 at 15:32
 |  Show 3 more comments

1 Answer 1

Reset to default 0
//Create an array containing the IDs of pages that are children of page ID XXX
$args = array(
'post_type' => 'page',
'post_parent' => XXX,
'fields' => 'ids',
'posts_per_page' => -1,
);
$qry = new WP_Query($args);

//Convert the array into a string
$string = implode(', ', $qry->posts);

//Display the list of IDs - for testing purposes.
//var_dump($string);                

//Create a list of all pages excluding the list of child pages
wp_list_pages(array('exclude' => 'XXX,'.$string, 'title_li' => ''));
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749035355a315809.html

最新回复(0)