How to check if new posts have been published since page load?

Determining the current time

There’s the current_time() function in core that allows to pass three different values as first argument and gmt (the offset) as second argument. About the 1st:

  1. mysql -> gmdate( 'Y-m-d H:i:s' ), human readable e.g 2004/07/8 13:35:19

  2. timestamp -> time(), the UNIX timestamp for e.g. 1335939007

  3. A custom argument that is a valid formatting string for PHPs date() function,

    for e.g. D, d M Y H:i:s

Now we will just go with

$now = current_time( 'mysql' );

to not get the UNIX timestamp of (pretty much) now, as the WP_Query will need something that she can transform to a UNIX timestamp using strtotime().

Querying for newer posts

Then we will use the date_query clause with after to query posts later than now. I use the fields argument there to reduce the impact on the database. That means that the result will just be used to check if we got newer posts and only fetch the IDs instead of whole rows.

$newer_posts_IDs = new WP_Query( array(
    'post_type'  => 'post',
    'fields'     => 'ids',
    'date_query' => array(
        'after' => current_time( 'mysql' ),
    ),
) );

…then $newer_posts_IDs->found_posts will hold the amount of posts for the query.

Where to go from here?

What you consider to be “finished” is up to you. You could add above query/check to something like the jQuery ( document ).ready() event, add it to the shutdown hook or whatever pleases you.

Finally you will need to repeat above query with a whole set of rows, fetched via . Search through my answers on that topic.

You probably want to insert them on a specific point in DOM and add an empty container there as placeholder. jQuery offers the .before() API function in case you already got posts. Else you will probably just use .html() to insert your content.

Leave a Comment