• Hi guys, So i have this plugin that i had got developed a year ago called WP Custom Update(code below). What this does is if you enter your post id in the plugin it will simply update that post every 10, 20 or 30 minutes. So its basically refreshing your post like if you were to manually go into your post edit and hit the update button. I would like to change the functionality of this and instead of having to enter the post id i would like all post that gets published to the site to be updated automatically as if i just opened the post and hit update button. I also would like this update to only happen once instead of happening over and over again. Thanks

    Wp Custom Update (code)

    <?php
    /*
    Plugin Name: WP Custom Update
    Plugin URI: mailto:[email protected]
    Description: A plugin will update a set of post ids in a particular interval.
    Author: Thamizhchelvan
    Version: 1.0
    Author URI: https://thamizhchelvan.com/
    */
    
    //################### START OF ADMIN SETTINGS ################################################/
    
    function wpcu_menuitems(){
    	add_submenu_page('options-general.php', 'Custom Update', 'Custom Update Settings', 8, 'wpcu_custom_settings', 'wpcu_custom_settings');
    }
    
    add_action('admin_menu', 'wpcu_menuitems');
    
    function wpcu_custom_settings(){
    	if(isset($_POST['wpcu_thirty'])){
    
    		update_option('wpcu_thirty',$_POST['wpcu_thirty']);
    		update_option('wpcu_twenty',$_POST['wpcu_twenty']);
    		update_option('wpcu_ten',$_POST['wpcu_ten']);
    	}
    
    	?>
    <div class="icon32" id="icon-options-general"><br/></div>
    <h2>Custom Product Updates</h2>
    <BR />
    <center>
    <form method="post">
    <table width="100%">
    
    <tr><td>Post/Page IDs - 30 Minutes Interval</td><td><input class="regular-text" value="<?php echo get_option('wpcu_thirty');?>" type="text" name="wpcu_thirty" />[Enter seperated by comma] Example:1,3,45,67</td></tr>
    
    <tr><td>Post/Page IDs - 20 Minutes Interval</td><td><input class="regular-text" value="<?php echo get_option('wpcu_twenty');?>" type="text" name="wpcu_twenty" /></td></tr>
    
    <tr><td>Post/Page IDs - 10 Minutes Interval</td><td><input class="regular-text" value="<?php echo get_option('wpcu_ten');?>" type="text" name="wpcu_ten" /></td></tr>
    
    <tr><td></td>&nbsp;<td><input type="submit" value="Save" /></td></tr>
    </table>
    </form>
    </center>
    
    	<?php
    }
    
    //################### END OF ADMIN SETTINGS ################################################/
    add_filter( 'cron_schedules', 'wpcu_corn_schedules');
    function wpcu_corn_schedules(){
    	return array(
    		'in_per_ten_minute' => array(
    			'interval' => 60 * 10,
    			'display' => 'In every Ten Mintues'
    		),
    		'in_per_twenty_minute' => array(
    			'interval' => 60 * 20,
    			'display' => 'In every Twenty Mintues'
    		),
    		'in_per_thirty_minute' => array(
    			'interval' => 60 * 30,
    			'display' => 'In every Thirty Mintues'
    		)
    
    	);
    }
    
    if(!wp_next_scheduled('wpcu_ten_minute_event' )){
    	wp_schedule_event( time(), 'in_per_ten_minute', 'wpcu_ten_minute_event' );
    }
    
    if(!wp_next_scheduled('wpcu_twenty_minute_event' )){
    	wp_schedule_event( time(), 'in_per_twenty_minute', 'wpcu_twenty_minute_event' );
    }
    
    if(!wp_next_scheduled('wpcu_thirty_minute_event' )){
    	wp_schedule_event( time(), 'in_per_thirty_minute', 'wpcu_thirty_minute_event' );
    }
    
    add_action('wpcu_ten_minute_event', 'wpcu_update_ten');
    add_action('wpcu_twenty_minute_event', 'wpcu_update_twenty');
    add_action('wpcu_thirty_minute_event', 'wpcu_update_thirty');
    
    function wpcu_update_ten(){
    	wpcu_update_postids('wpcu_ten');
    }
    
    function wpcu_update_twenty(){
    	wpcu_update_postids('wpcu_twenty');
    }
    
    function wpcu_update_thirty(){
    	wpcu_update_postids('wpcu_thirty');
    }
    function wpcu_update_postids($interval){
    	$post_ids = get_option($interval);
    	if($post_ids != ''){
    		$postids = explode(",",$post_ids);
    		foreach($postids as $pid){
    			$postid = trim($pid);
    			$update_post = array();
    			$update_post['ID'] = $postid;
    			wp_update_post($update_post);
    			MPT_backoffice::create_thumb($postid);
    		}
    	}
    }
    ?>

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    Use WP_Query to get all published posts, loop through everything returned and call wp_update_post() on each one would be one way. You could probably submit a query through $wpdb to simply set the date columns to the current date of all published post rows. The direct query approach may not work if there’s any custom script run on post updates.

    Thread Starter sohinc

    (@sohinc)

    We have like 90k post on the site so it wouldnt be good to have the dates changed like that plus our site is about deals and products so it would not be good if dates got changed and old post came to the top… I dont want to make any changes to old post just the new post that are coming into the site needs to be updated automatically… to explain a bit more the reason we want to do this is because we are using feeds on our site to import post and there is a plugin called Auto Upload Images which takes the external image on your post and makes it a local image as if you uploaded the image manually on your server… But the only way Auto Upload Image works is if you are manually making posts and publishing it but it does not work for feed post… It will however work if you were to open the feed post and hit update. So im looking to achieve this functionality. Thanks for your response.

    Moderator bcworkz

    (@bcworkz)

    I would assume that Auto Upload has hooked into one of the actions that fire when the post is updated. The ideal solution would be to query the posts you wish to “update”, then directly call the Auto Upload callback that was hooked into the post update process. You could even use call_user_func_array() just as do_action() does, passing the same parameters as well. As far as the callback is concerned, it wouldn’t know the difference from your call and the action call. I doubt there’s any need to actually update the post.

    I’ve made a lot of assumptions, so something could easily prevent this from working. I think it’s worth doing a test using a single post and calling the callback function to see if it all works as I think it will.

    Hello Did you find a solution to your problem?

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘WP Custom Update – Want to update post automatically’ is closed to new replies.