admin管理员组文章数量:1125717
I want to make a filter list for my posts (in homepage, search page and archive page) Here is an example of what i'm looking for
Sort by: - Date (/?orderby=date) - Random (/?orderby=rand) - Views (here i need an orderby=views or something like that)
I'm counting and viewing post views by using this code:
// 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);
}
}
// Show views
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';
}
// Show views in WP-Admin
add_filter('manage_posts_columns', 'posts_column_views');
add_action('manage_posts_custom_column', 'posts_custom_column_views', 5, 2);
function posts_column_views($defaults) {
$defaults['post_views'] = __('Views');
return $defaults;
}
function posts_custom_column_views($column_name, $id){
if($column_name === 'post_views') {
echo getPostViews(get_the_ID());
}
}
Is this possible?
I want to make a filter list for my posts (in homepage, search page and archive page) Here is an example of what i'm looking for
Sort by: - Date (/?orderby=date) - Random (/?orderby=rand) - Views (here i need an orderby=views or something like that)
I'm counting and viewing post views by using this code:
// 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);
}
}
// Show views
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';
}
// Show views in WP-Admin
add_filter('manage_posts_columns', 'posts_column_views');
add_action('manage_posts_custom_column', 'posts_custom_column_views', 5, 2);
function posts_column_views($defaults) {
$defaults['post_views'] = __('Views');
return $defaults;
}
function posts_custom_column_views($column_name, $id){
if($column_name === 'post_views') {
echo getPostViews(get_the_ID());
}
}
Is this possible?
Share Improve this question edited Jun 5, 2017 at 1:27 Hank Scorpio asked Jun 4, 2017 at 21:54 Hank ScorpioHank Scorpio 1492 gold badges2 silver badges11 bronze badges 01 Answer
Reset to default 4You can sort your query by view number, using the meta key :
$query = new WP_Query(
array(
'meta_key' => 'post_views_count',
'orderby' => 'meta_value_num',
'order' => 'DESC',
)
);
This will query your posts based on the views. You can change DESC
to ASC
or RAND
to achieve what you asked.
To make it work in action, you can use pre_get_posts()
to filter your query. Use this piece code in your theme's functions.php
file:
add_action( 'pre_get_posts', 'my_view_filter' );
function my_view_filter($query){
if (
!is_admin() &&
$query->is_main_query() &&
( $query->is_home() || $query->is_archive() || $query->is_search() )
) {
if (isset($_REQUEST['orderby'])) {
$order = $_REQUEST['orderby'];
}
if ( $order === 'views') {
$query->set('meta_key', 'post_views_count');
$query->set('orderby', 'meta_value_num');
$query->set('order', 'DESC');
}
}
}
Now, whenever you visit http://example.com/?orderby=views
, your posts will be filtered by the number of views ( descending, you can change it to anything you want )
本文标签: orderOrderby Post Views
版权声明:本文标题:order - Orderby Post Views 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736675067a1947138.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论