sql >> Databasteknik >  >> RDS >> Mysql

Sida in Wordpress $wpdb Fråga?

Tack @jaziel matoso,

Nedanstående kod fungerar:

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;

Klart!



  1. MySQL WHERE IN () + AND , PDO returnerar endast en rad

  2. Hur man installerar Neo4j på Ubuntu 20.04

  3. Vad är skillnaden mellan Postgres DISTINCT vs DISTINCT ON?

  4. Hur man effektivt laddar mixed-type pandas DataFrame i en Oracle DB