Limit total number of post and post per page with get_results
Question
I’m using a custom table with a additional column, so I can’t use regular wp_query.
My code is working great except for the total number of post.
I want to display 5 results per page (which works great) but only display 20 post at total.
global $wpdb;
$items_per_page = 5;
only 5 posts per page
$page = isset( $_GET['cpage'] ) ? abs( (int) $_GET['cpage'] ) : 1;
$offset = ( $page * $items_per_page ) - $items_per_page;
$query = "SELECT post_id FROM $wpdb->postmeta WHERE country = 'fr'";
$total_query = "SELECT COUNT(1) FROM (${query}) AS combined_table";
$total = '20';
//$total = '$wpdb->get_var( $total_query )';
I tried to put 20. But if you put manually ‘5’ in your url, then
you see the other results.
$results = $wpdb->get_results( $query.' ORDER BY traffic DESC LIMIT '. $offset.', '. $items_per_page);
foreach ( $results as $result ) {
echo get_the_title( $result->post_id );
}
echo paginate_links( array(
'base' => add_query_arg( 'cpage', '%#%' ),
'format' => '',
'prev_text' => __('«'),
'next_text' => __('»'),
'total' => ceil($total / $items_per_page),
'current' => $page
));
0
mysql, sql, wpdb
4 years
2020-03-07T11:56:42-05:00
2020-03-07T11:56:42-05:00 0 Answers
88 views
0
Leave an answer