DataTables with Wordpress

DataTables with Wordpress

ddemuthddemuth Posts: 1Questions: 0Answers: 0

Trying to do serverside processing with Wordpress. I have the ajax action working but can't get pagination to work properly. Everything is still loading all at once.

[https://gist.github.com/ddemuth/4e483c2ef9e4f49c6bb4140e3070cce6](https://gist.github.com/ddemuth/4e483c2ef9e4f49c6bb4140e3070cce6 "https://gist.github.com/ddemuth/4e483c2ef9e4f49c6bb4140e3070cce6")

`add_action('wp_ajax_get_registrations_for_export', 'get_registrations_for_export');
add_action('wp_ajax_nopriv_get_registrations_for_export', 'get_registrations_for_export');

function get_registrations_for_export() {
    // echo '<pre>';print_r($_GET);exit;
    $post_statuses = array('unpaid', 'publish', 'cancelled');

    $args = array(
        'post_type' => 'fka_registrations', 
        'post_status' => $post_statuses,
        'posts_per_page' => 25,
        'no_found_rows' => true,
        'fields' => 'ids'
        );
        
    $argsCount = array(
        'post_type' => 'fka_registrations', 
        'post_status' => $post_statuses,
        'posts_per_page' => -1,
        'no_found_rows' => true,
        'fields' => 'ids'
        );

    $registrations_to_export = get_posts( $args);
    $registrations_count = get_posts( $argsCount);
    $total_registrations = count($registrations_count);
    //echo '<pre>';print_r($total_registrations);exit;
    $registrations = array();

    foreach ($registrations_to_export as $key => $registration_id) {

        
        $class_activity = get_field('activity', $registration_id);
        $activity_name = $class_activity->post_title;
        $activity_id = $class_activity->ID;


        $school_obj = get_field('school', $registration_id);
        if ($school_obj) {
            $school_name = $school_obj->post_title;
            $school_id = $school_obj->ID;
        }


        $session_obj = get_field('session', $registration_id);
        $session_name = $session_obj->name;
        $session_id = $session_obj->term_id;


        $product_info = get_page_by_title($school_name .' '. $activity_name, OBJECT, 'product');
        if ($product_info) {
            $fka_product_id = $product_info->ID;
        }

        $location = get_field('location_to_meet_coach', $fka_product_id);
        $class_day = get_field('class_day', $fka_product_id);


        $class_start_time = get_field('class_start_time', $fka_product_id);
        $class_end_time = get_field('class_end_time', $fka_product_id);
        $class_sessions = get_field('sessions', $fka_product_id);
        $name = get_the_title($registration_id);

        $array_key = fka_search_array($session_id, $class_sessions, $name);
        // echo '<pre>';print_r($array_key);exit;
        if ($array_key || $array_key == '0') {
            $session_start_date = $class_sessions[$array_key]['session_start_date'];
            $session_start_date = new DateTime($session_start_date);
            $session_end_date = $class_sessions[$array_key]['session_end_date'];
            $session_end_date = new DateTime($session_end_date);
        }
        $reg_post_status = get_post_status( $post);

        if ($reg_post_status == 'publish') {
            $reg_post_status = 'Paid';
        }
        $registrations[] = array(
            $name,
            $activity_name,
            $school_name,
            $class_day,
            $class_start_time .' - '. $class_end_time,
            $session_name,
            $location,
            'Test',
            'English',
            'hmmm',
            'English',
            'Dave',
            'Demuth',
            'Class',
            'Test',
            'English',
            'Dave',
            'Demuth',
            'Class',
            'Test',
            'English'
        );
    }

    $output['draw'] = 1;
    $output['recordsTotal'] = $total_registrations;
    $output['recordsFiltered'] = $total_registrations;
    $output['data'] = $registrations;

    wp_send_json($output);
}

Replies

  • soliddevsoliddev Posts: 1Questions: 0Answers: 0

    Were you able to get your Datatable working within WP? I am about to do the same type of thing, I'd appreciate any help or example you might offer.

  • allanallan Posts: 61,723Questions: 1Answers: 10,108 Site admin

    I don't see anything doing an OFFSET and LIMIT which would be required for server-side processing - specifically its paging. The above looks like it might just return everything.

    Allan

  • netstepincnetstepinc Posts: 3Questions: 0Answers: 0

    I'm going to try to implement this...but it might make my brain hurt a bit.
    https://activeengine.wordpress.com/2010/12/19/how-to-create-server-side-paging-for-datatables-net-with-asp-net/

  • netstepincnetstepinc Posts: 3Questions: 0Answers: 0

    I hacked together a WP-Datatables paging function. It's custom so I'm not sure it will be of much value to the community. However, let me know if you would like to see it to see if you can use it as a starting point.

This discussion has been closed.