简体   繁体   English

Wordpress-分页和自定义数据库查询

[英]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 对于我的查询,我没有使用带有args的常规wp_query,而是使用了自定义数据库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 它以我想要的顺序返回我的帖子,我只收到其中的5条

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. 修复后,我必须在自定义查询开始时添加SQL_CALC_FOUND_ROWS

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 并将我的max_num_paged传递给我的分页功能

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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