Shuffle the related-post results

Randomize the order of posts already selected by the Contextual Related Posts query.

What it changes

Contextual Related Posts first selects its results. This filter shuffles that result array before returning it.

Limits of the example

This does not select random posts from the entire site or expand the number of results. It only changes their order. Page or output caching may cause visitors to see the same ordering until that cached output is refreshed.

Requirements

Source

contextual-related-posts/crp-random-posts.php
<?php
/**
 * Randomly sort the related posts results.
 *
 * @package Contextual_Related_Posts
 */

/**
 * Randomly sort related posts after the query runs.
 *
 * The get_crp_posts_id filter was removed in CRP 3.3.0.
 * Use crp_query_the_posts instead, which receives a plain array of WP_Post objects.
 *
 * @since 1.0.0
 *
 * @param WP_Post[] $posts      Array of related post objects.
 * @param array     $query_args CRP query arguments.
 * @param WP_Query  $query      The WP_Query instance.
 * @param object    $crp_query  The CRP_Core_Query instance.
 *
 * @return WP_Post[] Shuffled array of post objects.
 */
function crp_truly_random( $posts, $query_args, $query, $crp_query ) {
	shuffle( $posts );
	return $posts;
}
add_filter( 'crp_query_the_posts', 'crp_truly_random', 99, 4 );

Details

Type
Function snippet
Last updated
GitHub
Source · Edit companion · History

Developer references