wpseek.com
Outil de recherche WordPress pour les développeurs et auteurs de thèmes



wp_random_content_pre_get_posts › WordPress Function

Depuis7.2.0
Dépréciéen/a
› wp_random_content_pre_get_posts ( $query )
Paramètres:
  • (WP_Query) $query The WP_Query instance (passed by reference).
    Requis: Oui
Défini(e) dans:
Codex:

Limits the main query to posts eligible for random content requests.

The main query is used to select the random post so the request runs the same query, and the same filters, as the archive it is randomizing. As such, callbacks on the {@see 'pre_get_posts'} action may modify the posts eligible for selection, for example to limit the post types or exclude a category: add_action( 'pre_get_posts', static function ( WP_Query $query ) { if ( wp_is_random_content_query( $query ) ) { $query->set( 'category__not_in', array( 1 ) ); } } ); Only randomable post types and published posts without a password are eligible. The number and order of posts are enforced by wp_random_content_post_limits and wp_random_content_posts_orderby, so callbacks modifying them on archives do not affect the selection. The random post IDs the post is selected from are stored in the random_sample_ids query variable, see wp_get_random_content_sample_ids. Added to the 'pre_get_posts'} action by {@see WP::handle_random while the random content query runs, so it runs after other callbacks.


Source

function wp_random_content_pre_get_posts( $query ): void {
	if ( ! wp_is_random_content_query( $query ) ) {
		return;
	}

	$query->set( 'post_type', wp_get_random_content_post_types( $query ) );
	$query->set( 'post_status', 'publish' );
	$query->set( 'has_password', false );
	$query->set( 'random_sample_ids', wp_get_random_content_sample_ids() );
	$query->set( 'ignore_sticky_posts', true );
	$query->set( 'no_found_rows', true );
	$query->set( 'update_post_meta_cache', false );
	$query->set( 'update_post_term_cache', false );
	// The order and number of posts are enforced by filters, which a suppressed query skips.
	$query->set( 'suppress_filters', false );
}