FEATURE Enlarging post numerotation handling to more post types and usiong a reusable function

This commit is contained in:
Antoine M 2026-02-24 15:04:43 +01:00
parent cea500ceda
commit 52f988c72d
2 changed files with 40 additions and 0 deletions

17
includes/posts-save.php Normal file
View File

@ -0,0 +1,17 @@
<?php
function handle_posts_numerotation_remapping_on_save()
{
$post_type = get_post_type();
$numerotated_post_types = array('analyses-etudes', 'expositions', 'outils-pedagogiques', 'recherches');
// Ne s'exécuter que dans l'admin pour éviter de surcharger le frontend
if (!is_admin() || !in_array($post_type, $numerotated_post_types)) {
return;
}
handle_posts_numerotation_remapping($post_type);
}
add_action('save_post', 'handle_posts_numerotation_remapping_on_save');
add_action('delete_post', 'handle_posts_numerotation_remapping_on_save');

View File

@ -62,6 +62,29 @@ function get_archive_page_subtitle_html($post_type)
return '';
}
}
function handle_posts_numerotation_remapping($post_type)
{
if (!is_admin()) {
return;
}
$query = new WP_Query(array(
'post_type' => $post_type,
'posts_per_page' => -1,
'orderby' => 'date',
'order' => 'ASC',
));
$numerotation = 0;
while ($query->have_posts()) {
$query->the_post();
$numerotation++;
update_post_meta(get_the_ID(), 'post_numerotation', $numerotation);
}
}
function hasPostTypeNumerotation($post_type)
{
switch ($post_type) {