2

I need to store custom post type post data into a custom table, not in wp_posts and wp_postmeta. Is it possible? anyone help please. Actually, I have created two custom post type named "truck" and "drivers". Then I have created a menu page in wordpress admin. Clicking this menu should list all drivers related to truck. There should be also drag and drop system of this interface so that I can assign any truck to any driver. After sorting, it should update table. So I need a new table for custom post type. see my screenshot

  • What is the reason behind this. This should be possible, but this will be tampering with wordpress innerworkings – Pieter Goosen Jul 19 '14 at 6:51
  • Actually, I have created two custom post type named "truck" and "drivers". Then I have created a menu page in wordpress admin. Clicking this menu should list all drivers related to truck. There should be also drag and drop system of this interface so that I can assign any truck to any driver. After sorting, it should update table. So I need a new table for custom post type. – Arshad Hussain Jul 19 '14 at 6:56
  • Please add this info where it belong, inside your answer. Please file an edit – Pieter Goosen Jul 19 '14 at 7:10
  • That still doesn't explain why you need to save post type into a different table. Relationship with a post to another post can be done with setting post ID on post_parent field. – Sisir Jul 19 '14 at 7:38
  • As you can see in my edited question. There are two custom post named "drivers" and "truck". I have listed both custom post type data under a menu named "Manage Drivers". So I want when I sort them or assign truck to any driver by simply dragging. Then it saves to database. Now What can I do? – Arshad Hussain Jul 19 '14 at 7:46
3
+50

While I am a big fan of tweaking wordpress as much as it could because it allows us to, I think the best way forward is to have the best algorithm before coding.

Reading your question, I came across " Actually, I have created two custom post type named 'truck' and 'drivers' ".

Issue: Linking two post types requires extra work as you are doing because natively it is not what Wordpress does.

Suggested Solution: Do it, the way WP works naturally: Link a Custom Post Type to a taxonomy. So, instead of having 'truck' and 'drivers' as CPTs, have 'drivers' as CPT and 'trucks' as taxonomy! That saves you tons of work, and especially the risk of running into issues should WP Update.

You can then create the custom functionality of dragging and dropping on CPT and taxonomy

  • That is the perfect answer I have got ever, no code, no logic but I got concept thank you very much – Arshad Hussain Jul 30 '14 at 4:37
  • @ArshadHussain I am glad you got the point immediately. If you need help with the code I can chip in too. I do this on a regular basis – Janvier Jul 31 '14 at 5:14
6

I am not going to give any specific code but I will give you Idea how it can be done without creating any custom database table. I am assuming one driver per truck

Algorithm

  1. Create two html list, make them sortable.
  2. Each list items represents a post type and have ID data field.
  3. Once sorting is done get all ids in order. Make two array one contains truck ids another driver ids.
  4. Send them via ajax.
  5. Loop through any of the array update posts post parent field so that,

       $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];
    

    .. and so on

HTML Hint

We have two table one is driver and one truck. We output the ID for the post type for each item.

<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

Assuming you are going to use js/jq sorting. Perhaps jquery ui sortable library. When any one of the item (truck/driver) get sorted. You go through the both list and gather post id in an order.

// 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 Handler:

Before we get into that. How I am going to track which truck belongs to which driver and Vice versa? I am going to use post tables post_parent field for that. Some can use post meta. That is also fine.

If Driver#1 is assigned to Truck #11 then

Driver #1 post parent = 11
Truck #11 post parent = 1

I know weird, they each will be each others children and parent in same time :)

Now that we have js code setup we can add php code for receiving the data and process;

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: Everytime driver assigned a new truck . You will need to update both driver and truck post types post_parent field.

This is generally how it can be done.

  • I like it, but I want to make the interjection not to (re-)use post_parent for this because reusing a native element like this, in my mind, screams for trouble, in the long run problems are pretty much inevitable by doing so. – Nicolai Jul 28 '14 at 15:46
  • 1
    you can use postmeta instead. – Sisir Jul 28 '14 at 15:52
  • Sure, like you said, just wanted to make this a bit clearer to others, because of what I said before. – Nicolai Jul 28 '14 at 15:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.