I was able to get custom data from Wordpress database by $wpdb->get_results like so $results = $wpdb->get_results( $query, OBJECT );
However i would like to paginate the data using paginate_links();
Which is presently displaying no data with pagination links, I think my error is probably within $results = $wpdb->get_results( $query.'ORDER BY id DESC LIMIT'. $offset.', '. $items_per_page, OBJECT );
My code:
global $wpdb;
$table_name = $wpdb->prefix . 'templates';
$items_per_page = 3;
$offset = ( $page * $items_per_page ) - $items_per_page;
$page = isset( $_GET['cpage'] ) ? abs( (int) $_GET['cpage'] ) : 1;
$query = 'SELECT * FROM '.$table_name;
$total_query = "SELECT COUNT(1) FROM (${query}) AS combined_table";
$total = $wpdb->get_var( $total_query );
$results = $wpdb->get_results( $query.'ORDER BY id DESC LIMIT'. $offset.', '. $items_per_page, OBJECT );
$results = $wpdb->get_results( $query, OBJECT );
if(!empty($results)) {
echo"<table class=\"table table-hover\">";
echo"<thead>";
echo"<tr>";
echo"<th>Id</th>";
echo"<th>Date</th>";
echo"<th>Name</th>";
echo"<th>Image src</th>";
echo"<th>Category</th>";
echo"<th>Preview Link</th>";
echo"<th>BuiltWith</th>";
echo"<th>Price</th>";
echo"</tr>";
echo"</thead>";
echo"<tbody>";
foreach($results as $row){
echo"<tr>";
echo"<td>". $row->id . "</td>";
echo"<td>". $row->tdateTime ."</td>";
echo"<td>". $row->tName ."</td>";
echo"<td>". $row->tName ."</td>";
echo"<td>". $row->tCategory ."</td>";
echo"<td>". $row->tPreview ."</td>";
echo"<td>". $row->tBuiltWith . "</td>";
echo"<td>". $row->tPrice ."</td>";
echo"</tr>";
}
echo"</tbody>";
echo"</table>";
}
echo paginate_links( array(
'base' => add_query_arg( 'cpage', '%#%' ),
'format' => '',
'prev_text' => __('«'),
'next_text' => __('»'),
'total' => ceil($total / $items_per_page),
'current' => $page
));
Try this code
Update
I've tested this and it works on my site. A few things:
Replace my $query with yours
global $wpdb (per your comment regarding global variables) since it's out of scope!
get_results() returns an object when not told otherwise (second parameter is the return type)
Here's the code: