WordPress.org

Ready to get started?Download WordPress

Forums

my custom plugin - updates post dates (1 post)

  1. Siouxsie
    Member
    Posted 1 year ago #

    HI peeps,
    I have a client that has contracted me to make a plugin that essentially auto update the post dates weekly.

    I decided to use modified_date to see if I can get that changed first. It checks daily and compares post dates and if it is over a week old it should update the date to this monday's date.

    I got pretty far with it but I am finding it hard to test.

    add_action('my_daily_event', 'update_jobdate');
    
    function check_updates() {
    	if ( !wp_next_scheduled( 'update_jobdate' ) ) {
    		wp_schedule_event( time(), 'daily', 'update_jobdate');
    	}
    }
    add_action('wp', 'check_updates');
    
    // do something everyday
    
    	function update_jobdates(){
        global $wpdb;
    
    //this monday
    $this_monday = strtotime('Monday this week');
    $newdate = $this_monday;
    $newgmt_time = get_gmt_from_date($newdate);
    $today = current_time('timestamp');
    // Get all jobs
    
    	$jobs_list = $wpdb->get_results("SELECT ID, post_date, post_modified, post_modified_gmt FROM $wpdb->posts WHERE cat = 'job_listing' AND post_status = 'publish' AND post_type = 'post'", ARRAY_A);
    
    if ( $job_list ) {
        foreach ($job_list as $job_dates) {
            // Get current date
    		$job_date = get_post_date( 'U' );
            // Get the days difference
    		$difference = human_time_diff($job_date, $today);
    
            if ($difference >= "6 days") {
    
             // Update posts
      $my_post = array();
      $my_post['ID'] = '%s';
      $my_post['post_modified_gmt'] = '$newgmt_time';
      $my_post['post_modified'] = '$newdate';
    
    // Update the post into the database
      wp_update_post( $my_post );
    
            }
    		else {
    			//do nothing
    			 return false;
    			}
        }
    }
    	}
    add_action( 'wp' , 'update_jobdates' );
    ?>

    Anything obvious?
    And how do I test?

    Thanks in advance guys.

Topic Closed

This topic has been closed to new replies.

About this Topic