Skip to content

Instantly share code, notes, and snippets.

@ajaydsouza
Created November 18, 2018 12:38
Show Gist options
  • Save ajaydsouza/8a92ffef3ca968bd2f906d3ed52dfa3e to your computer and use it in GitHub Desktop.
Save ajaydsouza/8a92ffef3ca968bd2f906d3ed52dfa3e to your computer and use it in GitHub Desktop.
Filter from and now date in Contextual Related Posts
<?php
/**
* Filter crp_posts_from_date.
*
* @param string $where The Minimum date of the WHERE clause of the query.
* @param int $id Post ID
*/
function crp_same_date_from( $where, $id ) {
global $wpdb, $post;
$post_time = get_the_time( 'U', $post->ID );
$from_date = $post_time;
$from_date = gmdate( 'Y-m-d 0:0:0' , $from_date );
$where = $wpdb->prepare( " AND $wpdb->posts.post_date >= '%s' ", $from_date );
return $where;
}
add_filter( 'crp_posts_from_date', 'crp_same_date_from', 10, 2 );
/**
* Filter crp_posts_from_date.
*
* @param string $where The Minimum date of the WHERE clause of the query.
* @param int $id Post ID
*/
function crp_same_date_now( $where, $id ) {
global $wpdb, $post;
$post_time = get_the_time( 'U', $post->ID );
$to_date = $post_time + DAY_IN_SECONDS;
$to_date = gmdate( 'Y-m-d 0:0:0' , $to_date );
$where = $wpdb->prepare( " AND $wpdb->posts.post_date < '%s' ", $to_date );
return $where;
}
add_filter( 'crp_posts_now_date', 'crp_same_date_now', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment