我不会给出任何具体的代码,但我会告诉您如何在不创建任何自定义数据库表的情况下完成这项工作。我假设one driver per truck
算法创建两个html列表,使其可排序每个列表项表示一个帖子类型,并具有ID数据字段排序完成后,按顺序获取所有ID。制作两个数组,一个包含卡车ID,另一个包含驾驶员ID通过ajax发送循环遍历任何数组update post post父字段,以便,
$truck[0]->post_parent = $driver[0]
$driver[0]->post_parent = $truck[0];
$truck[1]->post_parent = $driver[1]
$driver[1]->post_parent = $truck[1];
$truck[2]->post_parent = $driver[2]
$driver[2]->post_parent = $truck[2];
。。等等
我们有两个表,一个是司机表,一个是卡车表。我们为每个项目输出post类型的ID。
<h1>Drivers</h2>
<ul id="drivers">
<li data-id="1">Driver 1</li>
<li data-id="2">Driver 2</li>
<li data-id="3">Driver 3</li>
<li data-id="4">Driver 4</li>
<li data-id="5">Driver 5</li>
</ul>
<h1>Trucks</h2>
<ul id="trucks">
<li data-id="11">Truck 1</li>
<li data-id="12">Truck 2</li>
<li data-id="13">Truck 3</li>
<li data-id="14">Truck 4</li>
<li data-id="15">Truck 5</li>
</ul>
2。假设您要使用JS/JQ排序。也许jquery ui可排序库。当任何一个项目(卡车/司机)被分拣时。您遍历这两个列表并按顺序收集帖子id。
// after sorting complete run this function as a callback
function wpse155095_get_data(){
var drivers = [];
var trucks = [];
// get all drivers ID
$(\'#drivers li\').each(function(){
var id = $(this).data(\'id\');
drivers.push(id);
});
// get all trucks ID
$(\'#trucks li\').each(function(){
var id = $(this).data(\'id\');
trucks.push(id);
});
// Now Send both data to server via ajax call
drivers = JSON.stringify(drivers); // array to json conversion
trucks = JSON.stringify(trucks); // array to json conversion
var data = {};
data.action = \'wpse155095_save_data\';
data.trucks = trucks;
data.drivers = drivers;
// important: I\'ve omitted nonce for simplicity. You should pass nonce via ajax check when receive data on ajax callback.
$.ajax({
url: ajaxurl, // in admin area ajaxurl is defined.
type: \'POST\',
data: data
});
}
3。AJAX处理程序:
在我们开始之前。
How I am going to track 哪个卡车属于哪个驾驶员,反之亦然?我要用邮局的桌子
post_parent
该字段。有些人可以使用post-meta。这也很好。
如果Driver#1
已分配给Truck #11
然后
Driver #1 post parent = 11
Truck #11 post parent = 1
我知道很奇怪,他们会同时成为彼此的孩子和父母:)
现在我们已经有了js代码设置,我们可以添加php代码来接收数据和流程;
add_action( \'wp_ajax_wpse155095_save_data\', \'ajax_wpse155095_save_data\' );
function ajax_wpse155095_save_data(){
// check nonce or other validation
$drivers = ( isset($_POST[\'drivers\']) && !empty($_POST[\'drivers\']) ) ? json_decode($_POST[\'drivers\'], true) : false;
$trucks = ( isset($_POST[\'trucks\']) && !empty($_POST[\'trucks\']) ) ? json_decode($_POST[\'trucks\'], true) : false;
if( !$drivers || !$trucks )
die(0); // or show some other error;
if( count($drivers) != count($trucks) )
die(\'count mismatch\'); // or show some other error
for( $i = 0; $i < count($drivers) ; $i++ ){
$driver_id = $drivers[$i];
$truck_id = $truck[$i]
// everytime driver or truck changes. You will need to update both driver and truck field
wp_update_post(array(\'ID\' => $driver_id, \'post_parent\' => $truck_id ));
wp_update_post(array(\'ID\' => $truck_id, \'post_parent\' => $driver_id ));
}
die();
}
Note: 每次司机都会分配一辆新卡车。您需要更新驾驶员和卡车立柱类型
post_parent
领域
这通常是可以做到的。