简体   繁体   中英

Wordpress - Pagination and Custom Database Query

I would like to have a pagination on a page where multiple custom post are listed. For my query, I am not using the usual wp_query with args, I am using a custom database select

Here is my query

$query = "SELECT distinct($wpdb->posts.id), omvp_posts.* FROM $wpdb->posts
        inner JOIN $wpdb->postmeta AS rating ON( $wpdb->posts.ID = rating.post_id AND rating.meta_key = 'omvp_vote_rating')
        inner JOIN $wpdb->postmeta AS vote ON( $wpdb->posts.ID = vote.post_id AND vote.meta_key = 'omvp_total_vote')
        inner JOIN $wpdb->term_relationships ON ( $wpdb->posts.ID = $wpdb->term_relationships.object_id )
        WHERE $wpdb->posts.post_status = 'publish'
        AND $wpdb->posts.post_type = 'guides'
        " . $CategoryQuery . "
        ORDER BY rating.meta_value, vote.meta_value DESC LIMIT 0,5";

It's returning my posts, with the order I want, and I only get 5 of them

To loop trought my result, I use this code:

$pageposts = $wpdb->get_results($query, object); 
if ($pageposts):
global $post;
foreach ($pageposts as $post):
setup_postdata($post); 

For my navigation, I was previously using this function, but I think since I am using a custom query, this function dosen't work anymore

function wp_numeric_pagination() {
    global $wp_query;

    $big = 999999999;
    $search_for   = array( $big, '#038;' );
    $replace_with = array( '%#%', '' );
    $tag = '<div class="pagination">' . PHP_EOL;
    $tag .= paginate_links( array(
            'base'              => str_replace( $search_for, $replace_with, esc_url( get_pagenum_link( $big ) ) ),
            'format'            => '?paged=%#%',
            'current'           => max( 1, get_query_var('paged') ),
            'total'             => $wp_query->max_num_pages,
            'prev_next'         => True,
            'prev_text'         => __('«'),
            'next_text'         => __('»'),
            'before_page_number' => '<span>',
            'after_page_number'  => '</span>'
        ) ) . PHP_EOL;

    $tag .= '</div>' . PHP_EOL;
    echo $tag;
}

Thank you !

Got it fixed, I had to add SQL_CALC_FOUND_ROWS at the start of my custom query.

Declared those variables over my query

    $paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
    $post_per_page = intval(get_query_var('posts_per_page'));
    $offset = ($paged - 1)*$post_per_page;

So my new query is

$query = "SELECT SQL_CALC_FOUND_ROWS distinct($wpdb->posts.id), omvp_posts.* FROM $wpdb->posts
        inner JOIN $wpdb->postmeta AS rating ON( $wpdb->posts.ID = rating.post_id AND rating.meta_key = 'omvp_vote_rating')
        inner JOIN $wpdb->postmeta AS vote ON( $wpdb->posts.ID = vote.post_id AND vote.meta_key = 'omvp_total_vote')
        inner JOIN $wpdb->term_relationships ON ( $wpdb->posts.ID = $wpdb->term_relationships.object_id )
        WHERE $wpdb->posts.post_status = 'publish'
        AND $wpdb->posts.post_type = 'guides'
        " . $CategoryQuery . "
        ORDER BY rating.meta_value, vote.meta_value DESC 
        LIMIT " . $offset . ", " . $post_per_page;

Added some code over my loop

/* Determine the total of results found to calculate the max_num_pages
for navigation */
$sql_posts_total = $wpdb->get_var( "SELECT FOUND_ROWS();" );
$max_num_pages = ceil($sql_posts_total / $post_per_page);

if ($pageposts):
global $post;
foreach ($pageposts as $post):

and passed my max_num_paged to my pagination function

function wp_numeric_pagination($max_num_pages) {
global $wp_query;

$big = 999999999;
$search_for   = array( $big, '#038;' );
$replace_with = array( '%#%', '' );
$tag = '<div class="pagination">' . PHP_EOL;
$tag .= paginate_links( array(
        'base'              => str_replace( $search_for, $replace_with, esc_url( get_pagenum_link( $big ) ) ),
        'format'            => '?paged=%#%',
        'current'           => max( 1, get_query_var('paged') ),
        'total'             => $max_num_pages,
        'prev_next'         => True,
        'prev_text'         => __('«'),
        'next_text'         => __('»'),
        'before_page_number' => '<span>',
        'after_page_number'  => '</span>'
    ) ) . PHP_EOL;

$tag .= '</div>' . PHP_EOL;
echo $tag;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM