wpseek.com
Uma área de pesquisa sobre o WordPress para devs e autores do tema



wp_get_random_content_sample_ids › WordPress Function

Desde7.2.0
Obsoleton/a
› wp_get_random_content_sample_ids ( Sem parâmetros )
Retorna:
  • (int[]) Up to 50 distinct post IDs. Posts with the IDs may not exist.
Definido em:
Codex:

Retrieves random post IDs for the random content query to select a post from.

Sorting posts randomly requires the database to read every eligible post. Instead, the random content query looks up 50 random post IDs, each by the primary key, and selects one of those that is an eligible post. The cost of the lookup does not depend on the number of posts, and as every post ID is equally likely to be looked up, every eligible post is equally likely to be selected. This is the case whether or not there are gaps between the IDs of posts. The IDs are between 1 and the highest post ID, which is stored in the object cache for 10 minutes. If there are no more than 50 post IDs, all of them are returned.


Fonte

function wp_get_random_content_sample_ids(): array {
	global $wpdb;

	$max_id = wp_cache_get( 'max_post_id', 'random-content' );

	if ( ! is_int( $max_id ) ) {
		$max_id = (int) $wpdb->get_var( "SELECT MAX(ID) FROM {$wpdb->posts}" );
		wp_cache_set( 'max_post_id', $max_id, 'random-content', 10 * MINUTE_IN_SECONDS );
	}

	if ( $max_id <= 50 ) {
		return range( 1, max( 1, $max_id ) );
	}

	$post_ids = array();

	while ( count( $post_ids ) < 50 ) {
		$post_ids[ wp_rand( 1, $max_id ) ] = true;
	}

	return array_keys( $post_ids );
}