• Resolved ktsiterip

    (@ktsiterip)


    Hi!
    Its probably extremley obvious, but is there a way to exclude posts by age – ie do not display posts that are more than a year old?
    For whatever reason we get some old posts appearing aand as we are a news site, we dont want to display them
    I have a data collection range of 1 day – but dont want to ” Display only posts published within the selected Time Range”
    Apologies if I am missing something obvious!

Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Author Hector Cabrera

    (@hcabrera)

    Hi there,

    That’s exactly what “Display only posts published within the selected Time Range” is for.

    Thread Starter ktsiterip

    (@ktsiterip)

    Hi Hector
    As I say its probably my stupidity(!)
    Here is a screenshot of the 2 settings
    https://imgur.com/a/tSShExK

    So “Time Range” is set to one day. My understanding is that this means that it will only display post hits for the last day. So if a post that is say 6 months old is visted in the last 24 hours, it will display in the list – which is great and what I want

    But, if I click “Display only posts published within the selected Time Range”, surely I will only be able to see posts that were published in the last 24 hours?

    Am I approaching this in the wrong way?

    Aplolgies for such a stupid question!

    • This reply was modified 6 years, 1 month ago by ktsiterip.
    Plugin Author Hector Cabrera

    (@hcabrera)

    Ah, thanks for the clarification. Originally you said:

    I have a data collection range of 1 day (…)

    so I thought you were talking about the Data Logging feature. I see what you mean now.

    Unfortunately, the plugin doesn’t offer such a feature. There might be a way to do it via filter hooks, though. I’ll play around with this idea and if it works I’ll tell you what you need to do.

    Aplolgies for such a stupid question!

    There’s no such thing as a stupid question, so don’t worry about it.

    Plugin Author Hector Cabrera

    (@hcabrera)

    This seems to work:

    /**
     * Adds a time constraint condition so WPP returns only posts that
     * are up to 6 months old.
     *
     * @param string $where Original WHERE clause
     * @param array $options Plugin settings
     * @return string The filtered WHERE clause
     */
    function wpp_limit_post_age( $where, $options ){
    
        if ( ! is_admin() ) {
            $where .= " AND p.post_date > DATE_SUB(NOW(), INTERVAL 6 MONTH) ";	
            return $where;
        }
    
        return $where;
    }
    add_filter( 'wpp_query_where', 'wpp_limit_post_age', 10, 2 );

    Basically, we’re using an undocumented filter hook to modify the query WPP uses to retrieve posts from your database.

    Since you want to display posts that are no more than a year old, you need to change INTERVAL 6 MONTH to INTERVAL 12 MONTH (or INTERVAL 1 YEAR).

    Note that said filter hook has been left undocumented for a very good reason: I may make changes to the original query (as I just did) which in turn might break your customization, breaking the query and thus the widget completely in the process. This mod, however, should be safe. I honestly don’t think you need to worry about it.

    Also, for this mod to work you’ll want to keep the “Display only posts published within the selected Time Range” option unchecked.

    And, in case you’re wondering, you need to add this code snippet to your theme’s functions.php file.

    Thread Starter ktsiterip

    (@ktsiterip)

    Hi Hector!
    Wow!
    Thanks for that – Have implemented it in functions.php – it dosent immediatley seem to have an effect – I’ve flushed both site cache and Popular Posts cache, but I’m still seeing older posts then 6 months….

    Plugin Author Hector Cabrera

    (@hcabrera)

    Just checked again and it works for me, so:

    • Did you make sure that the “Display only posts published within the selected Time Range” option is unchecked?
    • Are you using WPP’s Data Caching?
    • Are you using the latest version of the plugin?
    Thread Starter ktsiterip

    (@ktsiterip)

    Hi Hector
    The awnsers are yes, yes and yes to your questions….and another yes – because it has started working now – doubtless it was some caching issue!

    So thank you so much for your help – you exemplify everything that is great about the wordpress community with your always helpful approach and replies.
    Thank you!

    Plugin Author Hector Cabrera

    (@hcabrera)

    Glad I could help, and thank you for the kind words!

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Time limit for dispalying posts’ is closed to new replies.