如何按会话随机显示类别帖子

时间:2017-06-28 作者:user3415122

目前,我的主页帖子显示为随机的,并按访问者会话进行安排,但在上次添加的类别中,我想在类似主页的类别中随机安排帖子

function filter_where($where = \'\') {
    $duration = \'-\' . of_get_option(\'frontpage_popularity_duration\') . \' days\';
    $where .= " AND post_date > \'" . date(\'Y-m-d\', strtotime($duration)) . "\'";
    if (of_get_option(\'frontpage_popularity\') == \'comments\') {
        $where .= \' AND comment_count != 0\';
    }
    return $where;
}

function random_posts_orderby($orderby_statement) {
    $seed = $_SESSION["pinc_rand"];

    if (empty($seed)) {
        $_SESSION["pinc_rand"] = rand();
    }

    $orderby_statement = \'RAND(\'.$seed.\')\';
    return $orderby_statement;  
}

if (is_home()) {
    if (\'likes\' == $popularity) {
        if (of_get_option(\'show_repins\') != \'disable\') {
            $args = array(
                \'meta_key\' => \'_Likes Count\',
                \'meta_compare\' => \'>\',
                \'meta_value\' => \'0\',
                \'orderby\' => \'meta_value_num\',
                \'order\' => \'DESC\',
                \'paged\' => $paged
            );              
        } else {
            $args = array(
                \'meta_key\' => \'_Likes Count\',
                \'meta_compare\' => \'>\',
                \'meta_value\' => \'0\',
                \'orderby\' => \'meta_value_num\',
                \'order\' => \'DESC\',
                \'paged\' => $paged,
                \'meta_query\' => array(
                    \'relation\' => \'OR\',
                    array(
                        \'key\' => \'_Original Post ID\',
                        \'compare\' => \'NOT EXISTS\'
                    ),
                    array(
                        \'key\' => \'_Original Post ID\',
                        \'value\' => \'deleted\'
                    )
                )
            );
        }
        add_filter(\'posts_where\', \'filter_where\');
        add_filter(\'posts_orderby\', \'pinc_meta_value_num_orderby\');
        query_posts($args);
        remove_filter(\'posts_where\', \'filter_where\');
        remove_filter(\'posts_orderby\', \'pinc_meta_value_num_orderby\');
    } else if ($popularity == \'repins\') {   
        if (of_get_option(\'show_repins\') != \'disable\') {
            $args = array(
                \'meta_key\' => \'_Repin Count\',
                \'meta_compare\' => \'>\',
                \'meta_value\' => \'0\',
                \'orderby\' => \'meta_value_num\',
                \'order\' => \'DESC\',
                \'paged\' => $paged
            );
        } else {
            $args = array(
                \'meta_key\' => \'_Repin Count\',
                \'meta_compare\' => \'>\',
                \'meta_value\' => \'0\',
                \'orderby\' => \'meta_value_num\',
                \'order\' => \'DESC\',
                \'paged\' => $paged,
                \'meta_query\' => array(
                    \'relation\' => \'OR\',
                    array(
                        \'key\' => \'_Original Post ID\',
                        \'compare\' => \'NOT EXISTS\'
                    ),
                    array(
                        \'key\' => \'_Original Post ID\',
                        \'value\' => \'deleted\'
                    )
                )
            );
        }
        add_filter(\'posts_where\', \'filter_where\');
        add_filter(\'posts_orderby\', \'pinc_meta_value_num_orderby\');
        query_posts($args);
        remove_filter(\'posts_where\', \'filter_where\');
        remove_filter(\'posts_orderby\', \'pinc_meta_value_num_orderby\');
    } else if ($popularity == \'comments\') { 
        if (of_get_option(\'show_repins\') != \'disable\') {
            $args = array(
                \'orderby\' => \'comment_count\',
                \'paged\' => $paged
            );
        } else {
            $args = array(
                \'orderby\' => \'comment_count\',
                \'paged\' => $paged,
                \'meta_query\' => array(
                    \'relation\' => \'OR\',
                    array(
                        \'key\' => \'_Original Post ID\',
                        \'compare\' => \'NOT EXISTS\'
                    ),
                    array(
                        \'key\' => \'_Original Post ID\',
                        \'value\' => \'deleted\'
                    )
                )
            );
        }
        add_filter(\'posts_where\', \'filter_where\');
        add_filter(\'posts_orderby\', \'pinc_comments_orderby\');
        query_posts($args);
        remove_filter(\'posts_where\', \'filter_where\');
        remove_filter(\'posts_orderby\', \'pinc_comments_orderby\');
    } else if ($popularity == \'random\') {           
        if (of_get_option(\'show_repins\') != \'disable\') {
            $args = array(
                \'paged\' => $paged
            );
        } else {
            $args = array(
                \'paged\' => $paged,
                \'meta_query\' => array(
                    \'relation\' => \'OR\',
                    array(
                        \'key\' => \'_Original Post ID\',
                        \'compare\' => \'NOT EXISTS\'
                    ),
                    array(
                        \'key\' => \'_Original Post ID\',
                        \'value\' => \'deleted\'
                    )
                )
            );
        }
        add_filter(\'posts_orderby\', \'random_posts_orderby\');
        query_posts($args);
        remove_filter(\'posts_orderby\', \'random_posts_orderby\');
    } else {
        if (of_get_option(\'show_repins\') != \'disable\') {
            $args = array(
                \'paged\' => $paged
            );
        } else {
            $args = array(
                \'paged\' => $paged,
                \'meta_query\' => array(
                    \'relation\' => \'OR\',
                    array(
                        \'key\' => \'_Original Post ID\',
                        \'compare\' => \'NOT EXISTS\'
                    ),
                    array(
                        \'key\' => \'_Original Post ID\',
                        \'value\' => \'deleted\'
                    )
                )
            );  
        }
        query_posts($args);
    }
}

if (is_category()) {
    $categories = array(get_query_var(\'cat\'));
    $subcats = get_categories(array(\'child_of\' => get_query_var(\'cat\')));
    if ($subcats) {
        foreach ($subcats as $subcat) {
            array_push($categories, $subcat->cat_ID);
        }
    }

    if (of_get_option(\'show_repins\') != \'disable\') {            
        $args = array(
            \'category__in\' => $categories,
            \'paged\' => $paged
        );
    } else {
        $args = array(
            \'category__in\' => $categories,
            \'paged\' => $paged,
            \'meta_query\' => array(
                \'relation\' => \'OR\',
                array(
                    \'key\' => \'_Original Post ID\',
                    \'compare\' => \'NOT EXISTS\'
                ),
                array(
                    \'key\' => \'_Original Post ID\',
                    \'value\' => \'deleted\'
                )
            )
        );  
    }
    query_posts($args); 
}

if (is_tag()) {
    if (of_get_option(\'show_repins\') != \'disable\') {
        $args = array(
            \'tag__in\' => get_query_var(\'tag_id\'),
            \'paged\' => $paged
        );
    } else {
        $args = array(
            \'tag__in\' => get_query_var(\'tag_id\'),
            \'paged\' => $paged,
            \'meta_query\' => array(
                \'relation\' => \'OR\',
                array(
                    \'key\' => \'_Original Post ID\',
                    \'compare\' => \'NOT EXISTS\'
                ),
                array(
                    \'key\' => \'_Original Post ID\',
                    \'value\' => \'deleted\'
                )
            )
        );  
    }
    query_posts($args); 
}

get_template_part(\'index\', \'masonry\');
get_footer();
?>

和此类别页面代码

1 个回复
SO网友:Tom J Nowell

您不需要复杂的过滤器来修改SQL查询,只需使用order 的参数WP_Query (避免query_posts, 上面的代码将触发多个未使用的查询,使页面速度非常慢)

$q = new WP_Query( [
    \'order\' => \'rand\'
] );
也可以传递整数种子:

$q = new WP_Query( [
    \'order\' => \'rand(5)\'
] );
但有一些重要的警告

性能很慢,数据库必须做大量额外的工作。相反,在PHP中随机选择要搜索的内容,然后再搜索,速度会快得多

其他性能问题

问题中的代码使用query_posts, 有时会覆盖主查询,然后覆盖新查询,然后再次覆盖它。单人间query_posts 所有检查后的通话速度会明显加快。A.pre_get_posts 过滤器将更快(消除从未使用过的原始WP查询)。

您还严重依赖后期元查询。除了随机排序之外,这是WordPress站点性能和扩展方面最糟糕的事情。我预计你的网站服务器将需要5-10秒来加载主页,可能会更长,这会导致严重的SEO和访问者保留问题。您还将面临向多个用户提供页面的主要问题,数据库将很难为4到5个以上的并发访问者执行这些查询。您的主机可能会因为CPU使用率高而与您联系,否则您将需要大量的主机硬件

也不可能缓存您正在做的事情。全页缓存系统无法处理此问题,对象缓存也无法处理,浏览器缓存甚至可能会妨碍您。

与其随机显示10篇帖子,不如尝试显示10篇最老的帖子,或10篇最新的帖子,或随机选择的年份中的10篇帖子,或评论最多的帖子,或最受欢迎的帖子。确定PHP中的哪一个并缓存结果。从10个预选查询中选择是另一个选项。

结束