WordPress 6.2 引入 search_columns 参数来控制在搜索查询中搜索哪些字段

以前,WP_Query::parse_query()方法的s参数搜索post_titlepost_excerpt, 和post_content字段,除了使用posts_search过滤器和手动调整 SQL外,无法控制这一点。

WordPress 6.2 添加了使用search_columns参数指定在执行查询时搜索哪些字段的功能。

目前,只允许使用默认的 post_titlepost_excerptpost_content 列,但它可能会在 WordPress 的后续版本中得到扩展。

search_columns 参数的默认值为:array( 'post_title', 'post_excerpt', 'post_content' )

下面的示例将搜索在其摘要(post_excerp列)中包含 foo 的帖子,排除post_titlepost_content列。

<?php
$my_query = new WP_Query(
    array(
        's'              => 'foo',
        'post_type'      => 'post',
        'search_columns' => array( 'post_excerpt' ),
    )
);

要了解更多信息,请查看这里。


【AD】搬瓦工限量套餐:POWERBOX-30-1536,美国洛杉矶DC99 CN2 GIA,年付仅$41.95!

【AD】美国洛杉矶CN2 VPS/香港CN2 VPS/日本CN2 VPS推荐,延迟低、稳定性高、免费备份_搬瓦工vps