Hi @matdeweb
Perhaps the best solution here would be creating a custom field that pulls only the post-year data and saves it as a value. This way, you can create a filter based on it in the form of a simple list of years.
Best Regards,
Victor
ok, good idea. I know hot to create custom field but how update automaticaly the post-year?
regards,
HI @matdeweb
That would require a custom code which will be fired on the post save/update.
Something like this (please note that this is not a tested code/solution this is just an example):
<?php
add_action('save_post', 'update_post_year_custom_field', 10, 3);
function update_post_year_custom_field($post_id, $post, $update) {
// Prevent autosave from triggering the update
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return;
}
// Check if this is a revision or an unsupported post type
if (wp_is_post_revision($post_id) || !in_array($post->post_type, ['post'])) {
return;
}
// Verify user permissions
if (!current_user_can('edit_post', $post_id)) {
return;
}
// Get the post's publish date
$post_date = get_the_date('Y-m-d', $post_id);
if ($post_date) {
// Extract the year
$year = date('Y', strtotime($post_date));
// Update the custom field 'post_year' with the year
update_post_meta($post_id, 'post_year', $year);
}
}
?>
Best regards
Victor
Oh thank you! It works very fine!!!