Skip to content

Instantly share code, notes, and snippets.

@tommcfarlin
Created September 25, 2017 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tommcfarlin/db581f4871687c7a11c514a47aa6f618 to your computer and use it in GitHub Desktop.
Save tommcfarlin/db581f4871687c7a11c514a47aa6f618 to your computer and use it in GitHub Desktop.
[WordPress]
<?php
private function find_all_events( $event_id ) {
global $wpdb;
$events = $wpdb->get_results(
$wpdb->prepare("
SELECT ID, post_content
FROM $wpdb->posts
WHERE post_parent = '%d'
AND post_status = 'publish'
", $event_id),
ARRAY_A
);
return $events;
}
<?php
public function update_future_events( $event_id ) {
$events = $this->find_all_events( $event_id );
$iteration = 1;
foreach ( $events as $event ) {
preg_match_all( '/(\d+)/', $anniversary['post_content'], $matches );
$original_year = $matches[0];
$original_year = intval( $original_year[0] );
$new_year = ( $original_year + $iteration );
$iteration++;
// See forth-coming gist.
}
}
<?php
public function update_future_events( $event_id ) {
$events = $this->find_all_events( $event_id );
$iteration = 1;
foreach ( $events as $event ) {
// See previous gist.
$event['post_content'] = str_ireplace( $original_year, $new_year, $event['post_content'] );
wp_update_post(array(
'ID' => $event['ID'],
'post_content' => $event['post_content'],
));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment