我之前有这个代码来展示帖子。
$query_args = array(
'post_type' => 'post',
'posts_per_page' => 4,
'meta_key' => 'Views',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'ignore_sticky_posts' => true
);
$the_query = new WP_Query( $query_args );
if( $the_query->have_posts() )
{
while( $the_query->have_posts() )
{
$the_query->the_post();
echo '<a class="popularPostLinks" href="' . get_the_permalink() . '" rel="external" target="_blank">' . get_the_post_thumbnail() . '
<br>
<div>
<p>' . get_the_title() . '</p>
</div>
</a>';
}
}
else
{
// No posts found.
}
// Restore original post data.
wp_reset_postdata();
但现在我正在使用我的自定义查询:
$getAllTimeRows = $wpdb->get_results( $wpdb->prepare( "SELECT post_id, SUM( post_value ) AS 'Views' FROM {$wpdb->prefix}PopularPosts GROUP BY post_id ORDER BY Views DESC LIMIT 4" ) );
我如何使用上面的数组结果来显示与WP Query如何显示帖子相同的帖子? $getAllTimeRows
包含显示(最多4个)帖子的行(最多4个)。见下面的数组。
print_r ( $getAllTimeRows )
Array
(
[0] => stdClass Object
(
[post_id] => 213
[Views] => 16
)
[1] => stdClass Object
(
[post_id] => 215
[Views] => 11
)
[2] => stdClass Object
(
[post_id] => 217
[Views] => 9
)
[3] => stdClass Object
(
[post_id] => 227
[Views] => 7
)
)