wpseek.com
Eine auf WordPress spezialiserte Suchmaschine für Entwickler und Theme-Autoren



wp_random_content_pre_get_posts › WordPress Function

Seit7.2.0
Veraltetn/v
› wp_random_content_pre_get_posts ( $query )
Parameter:
  • (WP_Query) $query The WP_Query instance (passed by reference).
    Erforderlich: Ja
Definiert in:
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. Added to the 'pre_get_posts'} action by {@see WP::handle_random while the random content query runs, so it runs after other callbacks.


Quellcode

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( '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 );
}