June 2, 2025

Sitemap

'page',
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => array(
'menu_order' => 'ASC',
'title' => 'ASC',
),
);

$pages = get_posts( $args );

if ( empty( $pages ) ) {
return;
}

$is_truthy = function( $value ) use ( &$is_truthy ) {
if ( is_array( $value ) ) {
foreach ( $value as $item ) {
if ( $is_truthy( $item ) ) {
return true;
}
}

return false;
}

$value = strtolower( trim( (string) $value ) );

return in_array( $value, array( '1', 'yes', 'true', 'on', 'noindex' ), true );
};

$is_searchwp_excluded = function( $post_id ) {
/*
* Based on your older module, SearchWP Exclude UI uses this meta key.
* The old logic excluded the page if the key exists.
*/
return metadata_exists( 'post', $post_id, '_searchwp_exclude' );
};

$is_smartcrawl_noindex = function( $post_id ) use ( $is_truthy ) {
$possible_meta_keys = array(
'_wds_meta-robots-noindex',
'wds_meta-robots-noindex',
);

foreach ( $possible_meta_keys as $meta_key ) {
$value = get_post_meta( $post_id, $meta_key, true );

if ( $is_truthy( $value ) ) {
return true;
}
}

return false;
};

$pages_by_id = array();
$children_by_parent = array();
$page_is_linkable = array();

foreach ( $pages as $page ) {
$pages_by_id[ $page->ID ] = $page;

$page_is_linkable[ $page->ID ] = ! (
post_password_required( $page->ID )
|| $is_searchwp_excluded( $page->ID )
|| $is_smartcrawl_noindex( $page->ID )
);
}

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

if ( $parent_id && ! isset( $pages_by_id[ $parent_id ] ) ) {
$parent_id = 0;
}

if ( ! isset( $children_by_parent[ $parent_id ] ) ) {
$children_by_parent[ $parent_id ] = array();
}

$children_by_parent[ $parent_id ][] = $page;
}

$sort_pages = function( &$pages_to_sort ) {
usort( $pages_to_sort, function( $a, $b ) {
$menu_order_compare = (int) $a->menu_order <=> (int) $b->menu_order;

if ( 0 !== $menu_order_compare ) {
return $menu_order_compare;
}

return strcasecmp( get_the_title( $a->ID ), get_the_title( $b->ID ) );
} );
};

foreach ( $children_by_parent as &$child_pages ) {
$sort_pages( $child_pages );
}

unset( $child_pages );

$has_visible_content = function( $page ) use ( &$has_visible_content, $children_by_parent, $page_is_linkable ) {
if ( ! empty( $page_is_linkable[ $page->ID ] ) ) {
return true;
}

if ( empty( $children_by_parent[ $page->ID ] ) ) {
return false;
}

foreach ( $children_by_parent[ $page->ID ] as $child ) {
if ( $has_visible_content( $child ) ) {
return true;
}
}

return false;
};

$count_visible_group = function( $page ) use ( &$count_visible_group, $children_by_parent, $has_visible_content ) {
if ( ! $has_visible_content( $page ) ) {
return 0;
}

$count = 1;

if ( ! empty( $children_by_parent[ $page->ID ] ) ) {
foreach ( $children_by_parent[ $page->ID ] as $child ) {
$count += $count_visible_group( $child );
}
}

return $count;
};

$root_pages = isset( $children_by_parent[0] ) ? $children_by_parent[0] : array();

$columns = array( array(), array() );
$column_counts = array( 0, 0 );

foreach ( $root_pages as $root_page ) {
if ( ! $has_visible_content( $root_page ) ) {
continue;
}

$target_column = $column_counts[0] <= $column_counts[1] ? 0 : 1; $group_count = $count_visible_group( $root_page ); $columns[ $target_column ][] = $root_page; $column_counts[ $target_column ] += $group_count; } $render_page_list = function( $pages_to_render, $depth = 0 ) use ( &$render_page_list, $children_by_parent, $page_is_linkable, $has_visible_content ) { if ( empty( $pages_to_render ) ) { return; } $ul_classes = 0 === $depth ? 'list-unstyled mb-0' : 'list-unstyled mt-2 mb-0 ps-3 border-start'; echo '

    ';

    foreach ( $pages_to_render as $page ) {
    if ( ! $has_visible_content( $page ) ) {
    continue;
    }

    $is_linkable = ! empty( $page_is_linkable[ $page->ID ] );
    $has_children = ! empty( $children_by_parent[ $page->ID ] );

    $li_classes = 0 === $depth ? 'mb-4' : 'mb-2';

    if ( 0 === $depth ) {
    $text_classes = 'd-inline-block';
    } elseif ( $has_children ) {
    $text_classes = 'd-inline-block';
    } else {
    $text_classes = 'd-inline-block';
    }

    echo '

  • ';

    if ( $is_linkable ) {
    echo '';
    echo esc_html( get_the_title( $page->ID ) );
    echo '
    ';
    } else {
    echo '';
    echo esc_html( get_the_title( $page->ID ) );
    echo '
    ';
    }

    if ( $has_children ) {
    $render_page_list( $children_by_parent[ $page->ID ], $depth + 1 );
    }

    echo '

  • ';
    }

    echo '

';
};
?>

Categories