Last active
July 24, 2024 21:34
-
-
Save Qubadi/9183b46cb964c80df054560aae53b643 to your computer and use it in GitHub Desktop.
JetEngine Listing Grid: Custom Post Types, Drag-and-Drop Functionality on the Backend
This file contains hidden or 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
Video Tutorials here: | |
https://www.youtube.com/watch?v=hJhc90ETv0A | |
// Enqueue scripts and styles for the 'car' custom post type | |
add_action('admin_enqueue_scripts', 'enqueue_car_sortable_script_and_style'); | |
function enqueue_car_sortable_script_and_style($hook) { | |
global $typenow; | |
if ($typenow == 'car') { | |
wp_enqueue_script('jquery-ui-sortable'); | |
wp_localize_script('jquery-ui-sortable', 'car_sortable', array( | |
'nonce' => wp_create_nonce('car_sortable_nonce') | |
)); | |
echo '<style>#the-list tr td {cursor: move;} .column-numbering {width: 50px;}</style>'; | |
} | |
} | |
// Add a new column for numbering 'car' custom post type | |
add_filter('manage_car_posts_columns', 'add_car_numbering_column'); | |
function add_car_numbering_column($columns) { | |
$num_columns = array_slice($columns, 0, 1, true) + ['numbering' => 'No.'] + array_slice($columns, 1, NULL, true); | |
return $num_columns; | |
} | |
// Populate the new column for 'car' custom post type | |
add_action('manage_car_posts_custom_column', 'populate_car_numbering_column', 10, 2); | |
function populate_car_numbering_column($column, $post_id) { | |
static $row_number = 0; | |
if ('numbering' == $column) { | |
echo ++$row_number; | |
} | |
} | |
// Reset row number on each new page/load for 'car' custom post type | |
add_filter('views_edit-car', 'reset_car_row_number'); | |
function reset_car_row_number($views) { | |
global $current_screen; | |
if ($current_screen->post_type == 'car') { | |
remove_action('manage_car_posts_custom_column', 'populate_car_numbering_column', 10); | |
add_action('manage_car_posts_custom_column', 'populate_car_numbering_column', 10, 2); | |
} | |
return $views; | |
} | |
// Update the JavaScript for drag-and-drop and add renumbering functionality for 'car' custom post type | |
add_action('admin_footer', 'car_sortable_and_renumber_script'); | |
function car_sortable_and_renumber_script() { | |
global $typenow; | |
if ($typenow == 'car') { | |
?> | |
<script type="text/javascript"> | |
jQuery(document).ready(function($) { | |
var sortList = $('#the-list'); // Get the list | |
sortList.sortable({ | |
update: function(event, ui) { | |
$.ajax({ | |
url: ajaxurl, | |
type: 'POST', | |
dataType: 'json', | |
data: { | |
action: 'save_sort_car', | |
order: sortList.sortable('toArray').toString(), | |
nonce: car_sortable.nonce | |
}, | |
success: function() { | |
renumberRows(); | |
} | |
}); | |
}, | |
stop: function(event, ui) { | |
renumberRows(); | |
} | |
}); | |
// Function to renumber rows | |
function renumberRows() { | |
$('#the-list tr').each(function(index) { | |
$(this).find('td.column-numbering').html(index + 1); | |
}); | |
} | |
}); | |
</script> | |
<?php | |
} | |
} | |
// Handle AJAX request to save order for 'car' custom post type | |
add_action('wp_ajax_save_sort_car', 'save_sort_car'); | |
function save_sort_car() { | |
// Verify nonce | |
if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'car_sortable_nonce')) { | |
wp_die('Nonce verification failed'); | |
} | |
global $wpdb; | |
$order = explode(',', $_POST['order']); | |
foreach ($order as $index => $post_id) { | |
$post_id = str_replace('post-', '', $post_id); // Sanitize post ID | |
$wpdb->update($wpdb->posts, array('menu_order' => $index), array('ID' => $post_id)); | |
} | |
wp_die(); | |
} | |
// Modify the query on the 'car' custom post type admin page | |
add_action('pre_get_posts', 'car_custom_order'); | |
function car_custom_order($query) { | |
if (!is_admin() || $query->get('post_type') !== 'car') { | |
return; | |
} | |
$query->set('orderby', 'menu_order'); | |
$query->set('order', 'ASC'); | |
} | |
// Modify the front-end query for 'car' custom post type | |
function modify_car_query_on_front_end($query) { | |
if (!is_admin() && $query->is_main_query() && is_post_type_archive('car')) { | |
$query->set('orderby', 'menu_order'); | |
$query->set('order', 'ASC'); | |
} | |
} | |
add_action('pre_get_posts', 'modify_car_query_on_front_end'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment