post_name === $slug) { return $page['subpages']; } } // Return an empty array if the slug is not found return []; } // Return WordPress Page For A Slug function getPageForSlug($arrayOfWPPages, $slug) { foreach ($arrayOfWPPages as $page) { if ($page['page']->post_name === $slug) { return $page['page']; } foreach ($page['subpages'] as $index => $subpage) { if ($subpage->post_name === $slug) { return $page['subpages'][$index]; } } } // Return an empty array if the slug is not found return []; } function ddd($variable) { echo '
';
  var_dump($variable);
  echo '
';
}

// Get An Array of Top Level Pages and their Subpages
function get_pages_as_array()
{

  $args = [
    'sort_order'   => 'ASC',
    'sort_column'  => 'menu_order',
    'hierarchical' => 1,
    'exclude'      => []
  ];

  $pages = get_pages($args);
  $page_hierarchy = array();

  foreach ($pages as $page) {
    $parent_id = $page->post_parent;

    // If it's a top-level page
    if ($parent_id == 0) {
      $page_hierarchy[$page->ID] = array(
        'page' => $page,
        'subpages' => array()
      );
    } else {
      // If it's a child page, add it under its parent
      $page_hierarchy[$parent_id]['subpages'][] = $page;
    }
  }

  return $page_hierarchy;
}