Mysql
 sql >> Database >  >> RDS >> Mysql

Impaginare la query di Wordpress $wpdb?

Grazie a @jaziel matoso,

Sotto il codice funziona:

global $wp_query; 

$query = "
    SELECT wposts.* 
    FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta 
    WHERE wposts.ID = wpostmeta.post_id
        AND wpostmeta.meta_key = 'votes'
        AND wposts.post_status = 'publish'
        AND wposts.post_type = 'post'
    ORDER BY CAST(wpostmeta.meta_value AS SIGNED) DESC";

$total_record = count($wpdb->get_results($query, ARRAY_A));

$paged          = get_query_var('paged') ? get_query_var('paged') : 1;
$post_per_page  = get_option('posts_per_page');
$offset         = ($paged - 1)*$post_per_page;
$max_num_pages  = ceil($total_record/ $post_per_page);

$wp_query->found_posts   = $total_record;

// number of pages 
$wp_query->max_num_pages = $max_num_pages;

$limit_query = " LIMIT ".$post_per_page." OFFSET ".$offset; 

$result      = $wpdb->get_results($query.$limit_query,OBJECT);// return OBJECT

if($result):
foreach ($result as $post):
    setup_postdata($post); ?>

    <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
    <p><?php the_content(); ?></p>

<?php endforeach; ?>
    <div class="navigation"><?php wp_pagenavi(); ?></div>
    <?php
endif;

Fatto!