0

I have a WordPress website and have a page which shows all WordPress posts. Now I refresh the page every minute with javascript. But it is kind of annoying because I want it only to refresh when there is a update in the wp_posts or wp_postmeta table. Is there a function which I can call on an post update?

I read something about save_post. https://developer.wordpress.org/reference/hooks/save_post/

But this only runs on back-end post save. I want to run this function on the front-end after an administrator updates a post in the back-end so I can run a page reload inside this function on the front-end.

Maybe there is another function for this?

EDIT:

Before I have everything correctly working I want to know why this doesn't work:

$(document).ready(function() {

window.setInterval(function(){
    <?php
    global $wpdb;
    $sql = "SELECT * FROM `wp_posts` ORDER BY post_modified DESC LIMIT 1";
    $resultaten = $wpdb->get_results($sql);

    foreach ($resultaten as $resultaat) {
        $datumtijd = $resultaat->post_modified;
        $datum_en_tijd = date("d-m-Y H:i", strtotime($datumtijd)); ?>

        console.log("<?php echo $datum_en_tijd; ?>");
    <?php } ?> 
                
},10000);
});

This works fine and logs every 10 seconds but it returns the same every time while I updated a post in the backend. But if I use the query in phpMyAdmin it returns a different value each time so it looks like the query doesn't run again?

GrLTCode
  • 35
  • 6
  • I've built something similar, although not related to WordPress, maybe that works for you too: Get the result set, convert it into a JSON string and compare that JSON string to the old data with a strict comparison. If it matches, the data has not been updated and I'll not force a reload. Otherwise I already have the new data present, update the view and reload it. Not really a "WordPress way" but good maybe good enough. – flomei Jun 24 '20 at 09:10
  • You have any code to help me start up? – GrLTCode Jun 24 '20 at 09:13
  • So... you could use `save_post` to add a timestamp to a file, and then use JS/AJAX to check that file with your timestamp created upon loading - If the files timestamp is larger than yours, reload the page., then just run that AJAX call every 5 minutes or so. – Stender Jun 24 '20 at 09:14
  • Sounds as a good idea thanks! Let start that up. If I figure it out I will change my question to tell people how to do this – GrLTCode Jun 24 '20 at 09:22

1 Answers1

0

Just sketch how it can be.

// PHP ( functions.php )
// save updating timestamp to options
add_action('save_post', function(){
    if( DOING_AUTOSAVE )
        return;

    // maybe some filters here to setup updating only on specific post types & other cond.

    update_option( 'last_post_update', time(), true );
});
add_action('wp_ajax_check_post_updates', function(){
    do_action('wp_ajax_nopriv_check_post_updates');
});
add_action('wp_ajax_nopriv_check_post_updates', function(){
    $last_update = get_option('last_post_update', time());
    $last_updated = (int) $_POST['last_updated'];

    // no new posts / updates
    if( $last_update <= $last_updated )
        return;

    // get posts based on your options and return html 
});

And frontend

// JS
let last_updated = $('#pageWasLoadedAt').val(); // print hidden input somewhere with time() when page was loaded

setInterval(() => {
    const $container = $('#container-with-posts');
    $.ajax({
        dataType: 'html',
        data :{
            action: 'last_updated',
            last_updated: last_updated
        },
        complete: function(){ last_updated = Date.now(); },
        success:function(response){
            if( response ) $container.html(response);
        }
    });
Az Rieil
  • 176
  • 10