/**
* Process a single month: fetch posts, update cache, and create/update page
*
* @param string $month_slug e.g., 'january'
* @param string $tag_name e.g., 'January Births'
*/
function mbp_process_single_month($month_slug, $tag_name) {
$tag_slug = sanitize_title($tag_name);
$cache_key = MBP_CACHE_OPTION_PREFIX . $month_slug;
// Fetch latest 10 posts by tag and author
$new_posts = get_posts([
'post_type' => 'post',
'post_status' => 'publish',
'tag' => $tag_slug,
'author_name' => MBP_AUTHOR_SLUG,
'posts_per_page' => 10,
'orderby' => 'date',
'order' => 'DESC',
'fields' => 'ids',
]);
$cached_posts = get_option($cache_key, []);
$final_posts = (!empty($new_posts) && $new_posts !== $cached_posts) ? $new_posts : $cached_posts;
// Update cache if new posts are different
if (!empty($new_posts) && $new_posts !== $cached_posts) {
update_option($cache_key, $new_posts);
}
// Generate page content
$page_slug = MBP_PAGE_PATH_PREFIX . $month_slug . '-births';
$content = mbp_build_month_page_content($tag_name, $final_posts);
// FIXED: Better way to find existing page
$existing = mbp_find_existing_month_page($month_slug, $tag_name);
if ($existing) {
// Update existing page
$post_data = [
'ID' => $existing->ID,
'post_title' => $tag_name,
'post_content' => $content,
'post_status' => 'publish',
];
wp_update_post($post_data);
} else {
// Create new page
$post_data = [
'post_title' => $tag_name,
'post_name' => $page_slug,
'post_content' => $content,
'post_status' => 'publish',
'post_type' => 'page',
'post_author' => 1,
];
wp_insert_post($post_data);
}
}
/**
* Find existing month page by title or slug
*
* @param string $month_slug
* @param string $tag_name
* @return WP_Post|null
*/
function mbp_find_existing_month_page($month_slug, $tag_name) {
// Try to find by exact title first (most reliable)
$existing_by_title = get_page_by_title($tag_name, OBJECT, 'page');
if ($existing_by_title) {
return $existing_by_title;
}
// If not found by title, try by slug
$page_slug = MBP_PAGE_PATH_PREFIX . $month_slug . '-births';
$existing_by_slug = get_page_by_path($page_slug, OBJECT, 'page');
if ($existing_by_slug) {
return $existing_by_slug;
}
// Also check for variations with -2, -3, etc.
global $wpdb;
$like_slug = $wpdb->esc_like($page_slug) . '%';
$query = $wpdb->prepare("
SELECT ID FROM {$wpdb->posts}
WHERE post_type = 'page'
AND post_name LIKE %s
AND post_status = 'publish'
ORDER BY ID ASC
LIMIT 1
", $like_slug);
$page_id = $wpdb->get_var($query);
if ($page_id) {
return get_post($page_id);
}
return null;
}
Damián Rafael van der Vaart » Bio, Age, Net Worth, Family
Skip to the content
Home » Damián Rafael van der Vaart
May 7, 2022
Posted By
Abigail Anderson
Biography Sylvie Francoise Meis (born 13th April 1978) is also known as Sylvie van der Vaart. She is a Dutch…