sql >> Database >  >> RDS >> Mysql

Wordpress $wpdb-query pagineren?

Bedankt @jaziel matoso,

Onderstaande code werkt:

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;

Klaar!



  1. Een overzicht van Quests Nieuwste databasebewakingsservice - Spotlight Cloud

  2. In navolging van mysqli-query's

  3. ClassCastException:Integer kan niet naar Long worden gecast tijdens het herhalen van entiteits-ID's

  4. Kan op afstand verbinding worden gemaakt met SQL Server Express LocalDB?