Created
October 18, 2023 12:00
-
-
Save mennwebs/60a2e7cdda908dc32fc388009d4e9059 to your computer and use it in GitHub Desktop.
WP - Running No. Slug
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Running No. Slug | |
add_action('wp_insert_post', 'change_slug'); | |
function change_slug($post_id, $force = false) | |
{ | |
$post_types = ['news', 'faq']; | |
$post_type = $_POST['post_type']; | |
if (!in_array($post_type, $post_types) || get_field('no', $post_id)) { | |
return; | |
} | |
$i_no = new WP_Query([ | |
'post_type' => $post_type, | |
'posts_per_page' => 1, | |
'meta_key' => 'no', | |
'orderby' => 'meta_value_num', | |
'order' => 'DESC', | |
]); | |
while ($i_no->have_posts()) { | |
$i_no->the_post(); | |
$max = get_field('no'); | |
} | |
if (!$max) { | |
$max = 0; | |
} | |
$new_no = (int)$max + 1; | |
update_field('no', $new_no, $post_id); | |
wp_update_post(array( | |
'ID' => $post_id, | |
'post_name' => $new_no | |
)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment