wpseek.com
WordPress开发者和主题制作者的搜索引擎



wp_random_content_pre_get_posts › WordPress Function

Since7.2.0
已弃用n/a
› wp_random_content_pre_get_posts ( $query )
参数:
  • (WP_Query) $query The WP_Query instance (passed by reference).
    Required: Yes
定义在:
文档:

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.


源码

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