Skip to content

Instantly share code, notes, and snippets.

@itzikbenh
Last active January 9, 2020 20:12
Show Gist options
  • Select an option

  • Save itzikbenh/b22afda4672b0eadd979a074ef824c32 to your computer and use it in GitHub Desktop.

Select an option

Save itzikbenh/b22afda4672b0eadd979a074ef824c32 to your computer and use it in GitHub Desktop.
Datatables Sample
<?php
function athdata_get_companies( WP_REST_Request $request ) {
$draw = $request['draw'];
$start = $request['start']; //Start is the offset
$length = $request['length']; //How many records to show
$column = $request['order'][0]['column']; //Column to orderBy
$dir = $request['order'][0]['dir']; //Direction of orderBy
$searchValue = $request['search']['value']; //Text search value
$args = [
'posts_per_page' => $request['length'],
'orderby' => $column,
'order' => $dir ,
'post_type' => 'company',
'post_status' => 'publish',
'paged' => ($start / $length + 1)
];
$the_query = new WP_Query( $args );
return [
'draw' => $draw,
'recordsTotal' => $the_query->found_posts,
'recordsFiltered' => $the_query->found_posts,
'data' => $the_query->posts
];
}
<?php
function athdata_get_rounds( WP_REST_Request $request ) {
global $wpdb;
$rounds_table = $wpdb->prefix."company_rounds";
$posts_table = $wpdb->prefix."posts";
$draw = $request['draw'];
$start = $request['start']; //Start is the offset
$length = $request['length']; //How many records to show
$column = $request['order'][0]['column']; //Column to orderBy
$dir = $request['order'][0]['dir']; //Direction of orderBy
$searchValue = $request['search']['value']; //Text search value
$query = "SELECT SQL_CALC_FOUND_ROWS $posts_table.post_title, $rounds_table.round_number, $rounds_table.amount_raised, $rounds_table.type, DATE_FORMAT($rounds_table.raised_date, '%Y') as raised_date
FROM $rounds_table
INNER JOIN $posts_table
ON $rounds_table.post_id = $posts_table.ID
ORDER BY $posts_table.post_title ASC, $rounds_table.round_number ASC
LIMIT $length OFFSET $start";
$rounds = $wpdb->get_results($query);
$count = $wpdb->get_results('SELECT FOUND_ROWS() as count');
return [
'draw' => $draw,
'recordsTotal' => $count[0]->count,
'recordsFiltered' => $count[0]->count,
'data' => $rounds
];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment