Customize the related-post date range

Replace the Contextual Related Posts date query with a range derived from the current post's publication date.

What it changes

This example replaces the date query used by Contextual Related Posts. It derives an after date from the current post and a before date by adding one day, then enables inclusive bounds.

Check the boundaries

Despite the source comment describing a same-day restriction, both dates are inclusive. A date-only upper bound can include the following day. The example also mixes the post timestamp with gmdate(), so review your timezone and boundary requirements before relying on it for a precise calendar-day filter.

This replaces the existing date-query array rather than adding another condition to it.

Requirements

  • Contextual Related Posts
  • Contextual Related Posts must be active.
  • A current global post must be available when the query runs.

Source

contextual-related-posts/crp-change-query-dates.php
<?php
/**
 * This is a simple example that restricts CRP results to posts published on the same day as the current post.
 *
 * @package Contextual_Related_Posts
 */

/**
 * Restrict CRP related posts to those published on the same day as the current post.
 *
 * The crp_posts_from_date and crp_posts_now_date filters were removed in CRP 3.3.0.
 * Use crp_query_date_query instead, which accepts a standard WP_Query date_query array.
 *
 * @since 1.0.0
 *
 * @param array $date_query Date query array passed to WP_Query.
 *
 * @return array Updated date query.
 */
function crp_same_date_query( $date_query ) {
	global $post;

	$post_time = get_the_time( 'U', $post->ID );
	$from_date = gmdate( 'Y-m-d', $post_time );
	$to_date   = gmdate( 'Y-m-d', $post_time + DAY_IN_SECONDS );

	$date_query = array(
		array(
			'after'     => $from_date,
			'before'    => $to_date,
			'inclusive' => true,
		),
	);

	return $date_query;
}
add_filter( 'crp_query_date_query', 'crp_same_date_query', 10, 2 );

Details

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

Developer references