admin管理员组文章数量:1386550
I have a custom function in my theme that creates a dynamic sitemap.xml
file.
For that I'm using $myVar = get_posts( array(...) )
. In that array I need to exclude some pages.
I want to exclude some parent page and all its children pages.
I want to use only parent page slug to get an array of all the IDs of the parent and the children.
The problem: the 'exclude' =>
in the array()
accepts only an array of IDs. Not slugs.
How can I achieve it with some function (or some other way) to return an array of IDs by parent page slug including parent ID and all its children?
For the example let's say the parent page slug is abc
.
Thanks ahead.
I have a custom function in my theme that creates a dynamic sitemap.xml
file.
For that I'm using $myVar = get_posts( array(...) )
. In that array I need to exclude some pages.
I want to exclude some parent page and all its children pages.
I want to use only parent page slug to get an array of all the IDs of the parent and the children.
The problem: the 'exclude' =>
in the array()
accepts only an array of IDs. Not slugs.
How can I achieve it with some function (or some other way) to return an array of IDs by parent page slug including parent ID and all its children?
For the example let's say the parent page slug is abc
.
Thanks ahead.
Share Improve this question asked Apr 29, 2020 at 19:27 VictorVictor 254 bronze badges1 Answer
Reset to default 0You can use get_page_by_path()
to get a Page's WP_Post
object using its slug (ie, its path), and then use that object to then get the array of child page IDs using get_children()
.
// First we'll create the list of posts (pages) to exclude.
$exclude = array();
$parent_page_obj = get_page_by_path( 'abc' );
if ( ! empty( $parent_page_obj ) ) {
$parent_id = $parent_page_obj->ID;
// Adds the parent page to the $exclude array.
$exclude[] = $parent_id;
$args = array(
'post_type' => 'page',
'post_parent' => $parent_id,
'numberposts' => -1,
);
$children = get_children( $args );
foreach ( $children as $child ) {
$exclude[] = $child->ID;
}
}
// Now you can use the $exclude array in your get_posts() call.
$get_posts_arg = array(
// All your existing arguments here.
//...
'exclude' => $exclude,
);
$my_posts = get_posts( $get_post_args );
Get all children and grandchildren, yea, unto the nth generation
The code below uses recursion to get the children of the children, until you run out of children. I've tested it quickly in a local WP installation, and it worked to a depth of 5 generations.
It should work for what you're doing, but be aware that recursion can lead to infinite loops, so please test it in a non-production site before you put it into production.
/**
* Gets all the descendants of a give page slug.
*
* @param int|string $id The ID or slug of the topmost page.
* @param array $kids The array of kids already discovered. Optional.
* @return array The array of kids found in the current pass.
*/
function wpse365429_get_kids( $id, $kids = null ) {
if ( empty( $kids ) ) {
$kids = array();
}
if ( is_string( $id ) ) {
$obj = get_page_by_path( $id );
if ( ! empty( $obj ) ) {
$id = $obj->ID;
}
}
if ( ! in_array( $id, $kids ) ) {
$kids[] = $id;
}
$child_pages = get_children( $id );
if ( ! empty( $child_pages ) ) {
foreach ( $child_pages as $child ) {
$kids = wpse365429_get_kids( $child->ID, $kids );
}
}
return $kids;
}
$exclude = wpse365429_get_kids( 'abc' );
// From this point, you can use the $exclude array as you did in the
// previous code snippet.
本文标签:
版权声明:本文标题:get posts - How to get an array of pages ID by some page's slug and all its children pages in get_posts() function? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744500227a2609279.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论