wpseek.com
A WordPress-centric search engine for devs and theme authors
wp_get_random_content_post_types › WordPress Function
Since7.2.0
Deprecatedn/a
› wp_get_random_content_post_types ( $query )
| Parameters: |
|
| See: | |
| Returns: |
|
| Defined at: |
|
| Codex: |
Retrieves the post types eligible for selection by a random content request.
These are the post types the query would otherwise return, limited to those registered asrandomable.Source
function wp_get_random_content_post_types( WP_Query $query ): array {
$post_types = $query->get( 'post_type' );
if ( empty( $post_types ) && $query->is_tax() && $query->tax_query instanceof WP_Tax_Query ) {
// Match WP_Query::get_posts(): searchable post types using any of the queried taxonomies.
$taxonomies = array_keys( $query->tax_query->queried_terms );
$post_types = array();
foreach ( get_post_types( array( 'exclude_from_search' => false ) ) as $post_type ) {
$object_taxonomies = 'attachment' === $post_type ? get_taxonomies_for_attachments() : get_object_taxonomies( $post_type );
if ( array_intersect( $taxonomies, $object_taxonomies ) ) {
$post_types[] = $post_type;
}
}
} elseif ( empty( $post_types ) && ! $query->is_search() ) {
$post_types = array( 'post' );
}
if ( empty( $post_types ) || 'any' === $post_types ) {
$post_types = get_post_types( array( 'exclude_from_search' => false ) );
}
$random_post_types = array();
foreach ( (array) $post_types as $post_type ) {
// Plugins may set invalid values on the `post_type` query variable.
if ( ! is_string( $post_type ) ) {
continue;
}
$post_type_object = get_post_type_object( $post_type );
if ( $post_type_object && $post_type_object->randomable ) {
$random_post_types[] = $post_type_object->name;
}
}
return $random_post_types;
}