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?