Wordpress wp_查询而不是
我有这个功能,可以显示5个浏览次数最多的帖子 在functions.php中,我有:Wordpress wp_查询而不是,wordpress,Wordpress,我有这个功能,可以显示5个浏览次数最多的帖子 在functions.php中,我有: // function to display number of posts. function getPostViews($postID){ $count_key = 'post_views_count'; $count = get_post_meta($postID, $count_key, true); if($count==''){ delete_post_met
// function to display number of posts.
function getPostViews($postID){
$count_key = 'post_views_count';
$count = get_post_meta($postID, $count_key, true);
if($count==''){
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '0');
return "0 View";
}
return $count.' Views';
}
// function to count views.
function setPostViews($postID) {
$count_key = 'post_views_count';
$count = get_post_meta($postID, $count_key, true);
if($count==''){
$count = 0;
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '0');
}else{
$count++;
update_post_meta($postID, $count_key, $count);
}
}
目前,我正在使用一个query\u posts方法来显示5个浏览量最大的帖子:
<?php
query_posts( array (
'post_type' => 'post',
'showposts' => 5,
'meta_key' => 'post_views_count',
'orderby' => 'meta_value_num',
)
);
if (have_posts()) : while (have_posts()) : the_post();?>
<a href="<?php the_permalink();?>"><?php the_title();?></a>
<?php endwhile; endif; wp_reset_query(); ?>
现在我正试图通过使用wp_查询获得相同的结果,但它似乎不起作用
这是我用于wp\u查询的代码:
<?php $custom_query = new WP_Query('showposts=5, meta_key=post_views_count, orderby=meta_value_num'); // exclude category 9
while($custom_query->have_posts()) : $custom_query->the_post(); ?>
<a href="<?php the_permalink();?>"><?php the_title();?></a>
<?php endwhile; ?>
<?php wp_reset_postdata(); // reset the query ?>
它只显示5篇最新的文章,而不是5篇浏览量最多的文章。有人能帮我吗?将你的参数作为一个数组进行尝试:
$wpq_args = array(
'post_type' => 'post',
'showposts' => 5,
'meta_key' => 'post_views_count',
'orderby' => 'meta_value_num'
);
$custom_query = new WP_Query($wpq_args);
有关一些示例和所有参数,请参阅文档:showsposts不再是WP\u查询的有效参数,请从 每页文章数(int)-每页要显示的文章数(2.1版可用,替换了showposts参数) 正如lorem monkey所建议的,最好也将参数作为数组传递
<?php
$args = array('posts_per_page'=>5,'meta_key'=>'post_views_count','orderby'=>'meta_value_num');
$custom_query = new WP_Query($args);
while($custom_query->have_posts()) : $custom_query->the_post(); ?>
<a href="<?php the_permalink();?>"><?php the_title();?></a>
<?php endwhile; ?>
<?php wp_reset_postdata(); // reset the query
?>
很高兴它成功了!你介意打一下投票箭头旁边的复选标记,让未来的访问者知道这是对你有用的答案吗?