首页 > 解决方案 > 如何始终首先显示搜索结果中的特定帖子

问题描述

我创建了一个随机显示帖子的搜索过滤器,我想知道如何始终在搜索结果的第一个位置显示特定的帖子项目并正常显示其余项目。

感谢您的帮助@dassoun,这是查询:

$taxonomies = array('recipe_type', 'product_category', 'recipe_category', 'recipe_event', 'recipe_diet');
foreach ($taxonomies as $taxonomy) {
    $taxonomies_taxonomy[$taxonomy] = get_taxonomy($taxonomy);
    $taxonomies_terms[$taxonomy] = get_terms(
        array(
        'taxonomy'      => $taxonomy,
        'hide_empty'    => false,
        'meta_key'      => 'filter_order',
        'orderby'       => 'meta_value_num',
        'order'         => 'ASC'
        )
    );
    $taxonomies_selected[$taxonomy] = (get_query_var($taxonomy)) ? get_query_var($taxonomy) : '';
}

标签: phpwordpressfilterforeach

解决方案


我认为您应该看看“UNION”运算符。

来自https://www.w3schools.com/sql/sql_union.asp:UNION 运算符用于组合两个或多个 SELECT 语句的结果集。

  • UNION 中的每个 SELECT 语句必须具有相同数量的列
  • 列还必须具有相似的数据类型
  • 每个 SELECT 语句中的列也必须是相同的顺序

这个想法是:

select * from post where (hastobetop = 1 or id = myid or whatever)
UNION
yourfilteredrequest

您可能必须从过滤后的请求中排除第一个请求返回的行。

[编辑]对不起,我没有看到它是特定于 wordpress [/编辑]


推荐阅读