Support » Plugin: Contextual Related Posts » Related posts based on publish date

  • Resolved gstarter

    (@gstarter)


    Hi,
    I enjoy this plugin a lot, thank you!
    Is there an option to make the related posts based on the post publish date?
    For example, I have recipes on my website and I’d like to relate all the recipes published on the same date.

    Thanks,
    Guy.

    The page I need help with: [log in to see the link]

Viewing 15 replies - 1 through 15 (of 20 total)
  • Plugin Author Ajay

    (@ajay)

    Thread Starter gstarter

    (@gstarter)

    Thanks Ajay,
    But you are referring to the sorting, and I am referring to the related posts that appear on each post.
    I’d like the related post to be only the ones with the same publish date as the current post.
    Does it make sense?

    Cheers,
    Guy.

    Plugin Author Ajay

    (@ajay)

    Thanks. This does make sense. But, i don’t have an easy fix. You’ll need to filter the crp_posts_where clause instead.

    However, before I work out some code, do you have a lot of recipes that get published on the exact same day?

    Thread Starter gstarter

    (@gstarter)

    Thanks Ajay.
    Yes I do, I have 5-20 recipes published on the same day, and they should be related to each other.

    Thanks for your help

    Plugin Author Ajay

    (@ajay)

    Can you please try this (untested) piece of code in your theme’s functions.php. You’ll need to clear the CRP cache.

    
    function crp_same_date( $where, $id ) {
    
    	$where .= " AND post_date > '" . date( 'Y-m-d', strtotime( '-1 days' ) ) . "'";
    	$where .= " AND post_date < '" . date( 'Y-m-d', strtotime( '+1 days' ) ) . "'";
    
    	return $where;
    }
    add_filter( 'crp_posts_where', 'crp_same_date', 10, 2 );
    

    Could you please double check if it needs to be -1 or +1 or just 0 based on the results being fetched?

    Please backup your files before making these changes. Best option is to test on a staging site.

    Thread Starter gstarter

    (@gstarter)

    Hi Ajay,
    Thanks for sending the code.
    Other than adding the code to the functions.php file, was I supposed to do anything else?
    When I used the code as is no results were fetched …
    I tried -1, 1 and 0, none worked 🙁

    Thanks,
    Guy.

    Plugin Author Ajay

    (@ajay)

    Wondering if there is an error in the code, but shouldn’t be. Can you try this updated one.

    Also, can you install Query Monitor plugin and then view the queries being generated by this plugin. Tutorial here

    
    function crp_same_date( $where, $id ) {
    global $wpdb;
    
    	$where .= " AND $wpdb->posts.post_date > '" . date( 'Y-m-d', strtotime( '-1 days' ) ) . "'";
    	$where .= " AND $wpdb->posts.post_date < '" . date( 'Y-m-d', strtotime( '+1 days' ) ) . "'";
    
    	return $where;
    }
    add_filter( 'crp_posts_where', 'crp_same_date', 10, 2 );
    
    
    Thread Starter gstarter

    (@gstarter)

    Hi Ajay,
    This doesn’t work as well and there is no error in the code.
    I think I understand what the problem is:
    The recipes that should be returned from the query should have the same publish date as the current recipe, but only by the year month and day.
    This is almost there but the problem it compares a full dates and there is no other publish date that match the current post date …
    AND wp_posts.post_date = (select post_date from wp_posts where wp_posts.ID = $id)

    Could you refine the above?
    Thanks!

    Plugin Author Ajay

    (@ajay)

    Yes, that is the case as the post_date field which is why I used the above two lines. What is the error that the code is generating?

    Thread Starter gstarter

    (@gstarter)

    No Error, I just need you to fix the query so that it will work …

    The lines you used don’t make sense since no recipe will be in this date, you request the current date which is not relevant while you need to request the publish date of the recipe.

    Does it make sense?

    Plugin Author Ajay

    (@ajay)

    Of course! I’m not in a position to test, can you try this? I’ve passed an extra argument for the post date of the current post being viewed.

    
    function crp_same_date( $where, $id ) {
    global $wpdb, $post;
    
    	$where .= " AND $wpdb->posts.post_date > '" . date( 'Y-m-d', strtotime( '-1 days', $post->post_date ) ) . "'";
    	$where .= " AND $wpdb->posts.post_date < '" . date( 'Y-m-d', strtotime( '+1 days', $post->post_date ) ) . "'";
    
    	return $where;
    }
    add_filter( 'crp_posts_where', 'crp_same_date', 10, 2 );
    
    Thread Starter gstarter

    (@gstarter)

    Thanks!
    Now the additional conditions are added to the query:
    AND wp_posts.post_date > ‘1969-12-31’
    AND wp_posts.post_date < ‘1970-01-02’

    Looks like it is not the post date …

    Thread Starter gstarter

    (@gstarter)

    Moreover, there is a notice for the added lines:
    A non well formed numeric value encountered

    Plugin Author Ajay

    (@ajay)

    That is strange. Seems like the post_date isn’t returning properly, can you please just try get_the_date instead of the $post->post_date

    https://developer.wordpress.org/reference/functions/get_the_date/

    If this doesn’t work, I’ll run some tests on the weekend when I’ll be able to better access my dev systems

    Thread Starter gstarter

    (@gstarter)

    Thanks but it didn’t work.
    get_the_date returns a string …

Viewing 15 replies - 1 through 15 (of 20 total)
  • The topic ‘Related posts based on publish date’ is closed to new replies.