【问题标题】:Sorting by post views count doesnt work. Plugin: "Post Views Counter" - Wordpress按帖子浏览次数排序不起作用。插件:“发布视图计数器” - Wordpress
【发布时间】:2021-09-26 23:01:39
【问题描述】:

我正在制作新闻博客,我需要展示每日帖子。

我的博客中的每日帖子如何运作?:我会挑选当天的所有帖子并按观看次数对其进行排序。

问题是:我无法使用Post Views Counter 插件按观看次数排序。它不会返回任何关于 WP_Query 的帖子。

我的尝试:

function DisplayDailyNews(){
    $yesterday = date('d.m.Y',strtotime("yesterday"));
    $yesterdayarray = date_parse($yesterday);

    $dailyGridPostQuery = new WP_Query(array(
        "post_type"             => "post",
        "post_status "          => "publish",
        "posts_per_page"        => 3,
        "day"                   => $yesterdayarray["day"],
        "meta_key"              => "post_views_count",
        "orderby"               => "meta_value_num",
        "order"                 => "DESC"
    ));

    if ($dailyGridPostQuery->have_posts()){
        while ($dailyGridPostQuery->have_posts()){
            $dailyGridPostQuery->the_post(); 
            //Displaying post item here...
        } 
    }
}

我尝试用谷歌搜索解决方案,但没有一个对我有用。


FAQ1:是的,我今天有帖子。

【问题讨论】:

标签: php wordpress plugins


【解决方案1】:

此插件不使用自定义字段 post_views_count 来保存 post_count 它创建附加表

$wpdb->前缀。 "post_views

要按 post_views 排序帖子,请使用

"orderby" => "post_views",

试试这个代码

$dailyGridPostQuery = new WP_Query(array(
    "post_type"             => "post",
    "post_status "          => "publish",
    "posts_per_page"        => 3,
    "year"                  => $yesterdayarray["year"],
    "monthnum"              => $yesterdayarray["month"],
    "day"                   => $yesterdayarray["day"],
    "orderby"               => "post_views",
    "order"                 => "DESC"
));

附: post_views 必须 > 0

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多