Skip to content

Instantly share code, notes, and snippets.

@Qubadi
Created May 9, 2024 15:06
Show Gist options
  • Save Qubadi/cb2066e2184c58b73d3860b8f9a54325 to your computer and use it in GitHub Desktop.
Save Qubadi/cb2066e2184c58b73d3860b8f9a54325 to your computer and use it in GitHub Desktop.
Jetengine metabox: Dynamic number generator for CPT
Description:
This custom PHP snippet adds functionality to WordPress and Jetengine metabox, automatically assigning and updating
a sequence number for each post within a CPT. The sequence numbers are recalculated and updated dynamically whenever
a post is added, deleted, or trashed, ensuring that the newest post receives the number 1 and other posts are numbered
accordingly in descending order of their creation date. Additionally, the code implements a nonce for security,
ensuring its validity, particularly when used in form submissions or similar contexts.
1. Copy the custom code and create a PHP snippet. Paste the code into your snippet plugin.
2. First, create a Custom Post Type (CPT). Change the CPT name from "rent-car" to your preferred CPT slug name.
Search for "rent-car" in the custom code and replace all instances with your chosen CPT name.
3. Next, create a metabox and link it to your CPT.
4. Create a metafield and name it "generate-numbers.
5. Construct a listing grid and utilize dynamic fields. Set it to Meta data and locate "generate-numbers" as the source.
You'll then see the result. Design your listing grid, add the products you desire, and save it.
Use the listing grid widget to display the new listing grid.
_____________________________________________________-
// Function to add a custom meta box for posts
function add_custom_meta_box() {
add_meta_box(
'generate_number_meta_box',
'Generate Number',
'display_number_meta_box',
'rent-car', // Post type where the meta box will be added (Change 'rent-car' to your custom post type)
'normal',
'high'
);
}
add_action('add_meta_boxes', 'add_custom_meta_box');
// Callback function for displaying the meta box
function display_number_meta_box($post) {
// Nonce field for security, unique action name for nonce
wp_nonce_field('secure_action_generate_numbers', 'secure_nonce_generate_numbers');
$number = get_post_meta($post->ID, 'generate-numbers', true);
echo '<p>Post Number: ' . ($number ? $number : 'Number not set yet.') . '</p>';
}
// Function to update the generate number meta field for posts
function refresh_post_numbers() {
$args = array(
'post_type' => 'rent-car', // Specify your custom post type here
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'date',
'order' => 'DESC'
);
$query = new WP_Query($args);
$count = 1;
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
update_post_meta(get_the_ID(), 'generate-numbers', $count);
$count++;
}
}
wp_reset_postdata();
}
// Ensure numbers are updated after a post is fully inserted into the database
add_action('wp_after_insert_post', 'handle_post_insert_update', 10, 3);
// Function called after a post is inserted or updated
function handle_post_insert_update($post_id, $post, $update) {
if (!isset($_POST['secure_nonce_generate_numbers']) ||
!wp_verify_nonce($_POST['secure_nonce_generate_numbers'], 'secure_action_generate_numbers')) {
return;
}
if (wp_is_post_revision($post_id) || wp_is_post_autosave($post_id) ||
'rent-car' !== get_post_type($post)) { // Check custom post type
return;
}
if ('publish' === get_post_status($post_id)) {
refresh_post_numbers();
}
}
// Hooks to handle post deletion and trashing, ensuring immediate recalculations
add_action('deleted_post', 'handle_post_deletion');
add_action('trashed_post', 'handle_post_deletion');
// Function called when a post is deleted or trashed
function handle_post_deletion($post_id) {
if ('rent-car' === get_post_type($post_id) && !wp_is_post_revision($post_id)) { // Check custom post type
refresh_post_numbers();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment