文章查询条件修改扩展
通过 Filter_Plugin_ViewPost_Core 接口,可以在 Z-BlogPHP 的 ViewPost() 函数查询条件组装后、GetPostList 执行前修改查询条件,适用于排除指定分类的文章、调整相邻文章排序等场景。
接口信息
| 接口 | 参数 | 说明 |
|---|---|---|
Filter_Plugin_ViewPost_Core | &$select, &$w, &$order, $limit, $option | ViewPost() 查询条件组装后、GetPostList 执行前触发 |
完整案例
下例在文章页查询时排除分类 ID 为 5 的文章,访问这些文章将返回 404:
php
<?php
RegisterPlugin("demoAPP", "ActivePlugin_demoAPP");
function ActivePlugin_demoAPP()
{
Add_Filter_Plugin('Filter_Plugin_ViewPost_Core', 'demoAPP_ViewPost_Core');
}
function demoAPP_ViewPost_Core(&$select, &$w, &$order, $limit, $option)
{
$w[] = array('NOT IN', 'log_CateID', array(5));
}注意事项
- 参数
$select、$w、$order需要在回调签名中使用引用传递(&$w等),修改后才能对查询生效; - 该接口只在走
index.php前台入口的文章页、页面页触发,不作用于feed.php、search.php等独立入口; - 修改
$w等条件将直接影响最终 SQL;追加排除条件后,被排除的文章在前台访问时将查无此文; $option为查询附加选项数组,$limit为查询条数限制,一般保持原值即可。