Select parent page and all its child page but exclude one specific child page

Hi everyone hope all is good. I need some suggestion. I need to select parent page and all its child page but exclude one specific child page. can you please tell me how can i exclude specific child ?

```// Store Only Show For the Registered Users 
function woocommerce_store_private_redirect() {
global $post;
if (
    ! is_user_logged_in()
     (is_woocommerce() || is_cart() || is_checkout() || is_product_category() || is_product_tag() || is_product() || is_tree(64) )
) {
    // feel free to customize the following line to suit your needs
    wp_redirect( site_url('mein-konto/') );
    exit;
}
}
add_action('template_redirect', 'woocommerce_store_private_redirect');

function is_tree($pid) {      // $pid = The ID of the page we're looking for pages underneath
global $post;         // load details about this page

$pages = get_posts([
'post_type' = 'page',
'posts_per_page' = -1,
'post_parent' = $pid, // the parent id you want the children from
'post__not_in' = [4419] // the id you want to exclude
]);


if(is_page() $pages) 
           return true;   // we're at the page or at a sub page
else 
           return false;  // we're elsewhere
};

Topic development-strategy php Wordpress

Category Web


There are some issues in your code, e.g. $pages will always return an array, even where there are no results in which an empty array is returned, so if ( is_page() && $pages ) return true; does not necessarily mean that "we're at the page or at a sub page". Instead, it means, "as long as it's a Page (post type page), then we return true".

Nonetheless, try the following which works even if the page in question has a grandchild:

// This function checks whether the current Page has the specified ID ($pid), or
// that the current Page is a direct or indirect child of $pid.
function is_tree( $pid ) {
    // ID of the specific child page that you want to exclude.
    $cid = 4419;

    // Bail early if we're not on a Page.
    if ( ! is_page() ) {
        return false;
    }

    $page_id = get_queried_object_id();
    $pid     = (int) $pid;

    // We're on the parent page.
    if ( $pid === $page_id ) {
        return true;
    }

    // We're on the **excluded child page**, so return false.
    if ( (int) $cid === $page_id ) {
        return false;
    }

    $pages = get_pages( array( 'child_of' => $pid ) );

    // Check if we're on one of the other child pages.
    return in_array( $page_id, wp_list_pluck( $pages, 'ID' ) );
}

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.