How to get an array of pages ID by some page's slug and all its children pages in get_posts() function?

wordpress.stackexchange https://wordpress.stackexchange.com/questions/365429

Question

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.

Était-ce utile?

La solution

You 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.
Licencié sous: CC-BY-SA avec attribution
Non affilié à wordpress.stackexchange
scroll top