$args types, limit, order, show_date, headings.
*/
public static function render( array $args = array() ): string {
$types = $args['types'] ?? 'post,page';
$types = is_array( $types ) ? array_map( 'strval', $types ) : array_map( 'trim', explode( ',', (string) $types ) );
$types = array_values( array_filter( $types, static fn( string $type ): bool => post_type_exists( $type ) && is_post_type_viewable( $type ) ) );
if ( empty( $types ) ) {
return '';
}
$limit = max( 1, min( 2000, (int) ( $args['limit'] ?? self::DEFAULT_LIMIT ) ) );
$order = 'date' === ( $args['order'] ?? 'title' ) ? 'date' : 'title';
$show_date = ! empty( $args['show_date'] );
$headings = ! isset( $args['headings'] ) || ! empty( $args['headings'] );
$out = '
';
foreach ( $types as $type ) {
$posts = self::posts( $type, $limit, $order );
if ( empty( $posts ) ) {
continue;
}
if ( $headings ) {
$object = get_post_type_object( $type );
$label = $object ? (string) $object->labels->name : $type;
$out .= '
' . esc_html( $label ) . '
';
}
$out .= '
';
foreach ( $posts as $post ) {
$title = trim( wp_strip_all_tags( get_the_title( $post ) ) );
if ( '' === $title ) {
/* translators: %d: post ID of a page with no title. */
$title = sprintf( __( 'Untitled (#%d)', 'ai-rank-suite' ), (int) $post->ID );
}
$out .= '- ' . esc_html( $title ) . '';
if ( $show_date ) {
$out .= ' ' . esc_html( (string) get_the_date( '', $post ) ) . '';
}
$out .= '
';
}
$out .= '
';
}
return $out . '
';
}
/**
* The posts of one type that belong on a page meant to be crawled.
*
* Published only, and never one this plugin has been told to keep out of
* the sitemap or out of the index. Read as ids first, so a 10,000-post
* type costs one id query and one prime rather than ten thousand objects.
*
* @return array
*/
private static function posts( string $type, int $limit, string $order ): array {
// Ids first, then one query for all their meta, then the filtering in
// PHP. A meta_query with two OR groups joins the meta table twice and
// returns the same post once per matching row — the sitemap listed
// several pages twice, which is exactly the kind of thing a reader
// notices and a developer does not.
$candidates = get_posts(
array(
'post_type' => $type,
'post_status' => 'publish',
// Room for the ones that will be filtered out, bounded so a
// 50,000-post type is still one query for a few hundred ids.
'numberposts' => min( 5000, max( $limit * 3, $limit + 50 ) ),
'orderby' => 'date' === $order ? 'date' : 'title',
'order' => 'date' === $order ? 'DESC' : 'ASC',
'fields' => 'ids',
'ignore_sticky_posts' => true,
'no_found_rows' => true,
'update_post_term_cache' => false,
'suppress_filters' => false,
)
);
$candidates = array_map( 'intval', (array) $candidates );
if ( empty( $candidates ) ) {
return array();
}
update_meta_cache( 'post', $candidates );
$out = array();
foreach ( $candidates as $post_id ) {
if ( get_post_meta( $post_id, '_airanksuite_exclude_sitemap', true ) ) {
continue;
}
$robots = get_post_meta( $post_id, '_airanksuite_robots', true );
if ( is_array( $robots ) && in_array( 'noindex', array_map( 'strval', $robots ), true ) ) {
continue;
}
$post = get_post( $post_id );
if ( ! $post instanceof \WP_Post ) {
continue;
}
$out[] = $post;
if ( count( $out ) >= $limit ) {
break;
}
}
/**
* Filter the posts one type contributes to the HTML sitemap.
*
* @param array $out
* @param string $type
*/
return (array) apply_filters( 'airanksuite/html_sitemap/posts', $out, $type );
}
}