Drop the "group by post type" in search results
还没有人认领这个 Issue。
评估
调研方向
Start with acf_get_grouped_posts() in includes/api/api-helpers.php, where the search query's orderby and the _acf_orderby_post_type filter are applied, then with the search branch of the relationship field's AJAX query in includes/fields/class-acf-field-relationship.php. Reproduce by searching a relationship field across several post types and checking that results are grouped. Done when a search returns title matches first in a flat list, and the existing grouped behaviour still works for browsing.
由索引模型根据 Issue 内容生成。
描述
Describe the bug
A clear and concise description of what the bug is.
The grouping by post type makes it near impossible to find content in very large sites through the search feature in fields like the relationship field.
Instead of surfacing a title match, the results will always first show much weaker matcher in post types that happen to appear earlier.
To Reproduce
Steps to reproduce the behavior:
- Have a large site with many post types
- Have a relationship field that allows content from multiple post types
- Try searching for an exact title match for something that has a post type that isn't one of the earliest when sorting types.
- You will see much weaker matches, i.e. individual terms matched in body content.
- You need to scroll through all the weaker matches before finding your exact title match
Expected behavior
A clear and concise description of what you expected to happen.
I expect search to surface title matches first.
Version Information:
- WordPress Version 6.9
- PHP Version 8.4
- ACF Version latest
Suggested fix
diff --git a/includes/api/api-helpers.php b/includes/api/api-helpers.php
index c45f733b75..8a84c66f0e 100644
--- a/includes/api/api-helpers.php
+++ b/includes/api/api-helpers.php
@@ -1291,7 +1291,7 @@ function acf_get_grouped_posts( $args ) {
'posts_per_page' => -1,
'paged' => 0,
'post_type' => 'post',
- 'orderby' => 'menu_order title',
+ 'orderby' => empty( $args['s'] ) ? 'menu_order title' : 'relevance',
'order' => 'ASC',
'post_status' => 'any',
'suppress_filters' => false,
@@ -1364,7 +1364,7 @@ function acf_get_grouped_posts( $args ) {
}
// add filter to orderby post type
- if ( ! $is_single_post_type ) {
+ if ( ! $is_single_post_type && empty( $args['s'] ) ) {
add_filter( 'posts_orderby', '_acf_orderby_post_type', 10, 2 );
}
@@ -1372,10 +1372,30 @@ function acf_get_grouped_posts( $args ) {
$posts = get_posts( $args );
// remove this filter (only once)
- if ( ! $is_single_post_type ) {
+ if ( ! $is_single_post_type && empty( $args['s'] ) ) {
remove_filter( 'posts_orderby', '_acf_orderby_post_type', 10, 2 );
}
+ /*
+ * Optionally return every post as a single group, keyed by the caller supplied
+ * label, instead of one group per post type. Grouping partitions the results by
+ * post type, which discards the ordering the query itself established, so search
+ * callers that care about relevance (rather than browsing by type) opt out.
+ */
+ if ( ! empty( $args['acf_flat'] ) ) {
+ $this_group = array();
+
+ foreach ( $posts as $post ) {
+ $this_group[ $post->ID ] = $post;
+ }
+
+ if ( ! empty( $this_group ) ) {
+ $data[ $args['acf_flat'] ] = $this_group;
+ }
+
+ return $data;
+ }
+
// loop
foreach ( $post_types as $post_type ) {
diff --git a/includes/fields/class-acf-field-relationship.php b/includes/fields/class-acf-field-relationship.php
index 880175091f..0defe08314 100644
--- a/includes/fields/class-acf-field-relationship.php
+++ b/includes/fields/class-acf-field-relationship.php
@@ -266,6 +266,15 @@ if ( ! class_exists( 'acf_field_relationship' ) ) :
// Re-normalize in case a filter reset `post_status` to 'any' while leaving `perm=readable`.
$args = acf_ensure_perm_readable_post_status( $args );
+ /*
+ * While searching, skip the per post type grouping so that results stay in
+ * the order the query returned them (relevance), rather than being split
+ * into post type groups that each get their own optgroup heading.
+ */
+ if ( $is_search ) {
+ $args['acf_flat'] = 'search';
+ }
+
// get posts grouped by post type
$groups = acf_get_grouped_posts( $args );
@@ -306,7 +315,7 @@ if ( ! class_exists( 'acf_field_relationship' ) ) :
}
// add as optgroup or results
- if ( count( $args['post_type'] ) === 1 ) {
+ if ( count( $args['post_type'] ) === 1 || ! empty( $args['acf_flat'] ) ) {
$results = $results[0]['children'];
}
- 主要语言
- PHP
- 星标
- 947
- 派生
- 198
- PR 合并指标
- 30 天内没有已合并 PR
环境准备
这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
AdvancedCustomFields/acf 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 68/100
AdvancedCustomFields/acf#1056 ·
-
PHP 8.5: "Using null as an array offset" deprecation in ACF_Data::is() when rendering a block with empty data (local-meta.php is_request)可能已有人在做 @faisalahammad 于 30 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 68/100
AdvancedCustomFields/acf#1044 ·
-
is_preview was called incorrectly notice on every early get_field() since 6.8.10 (acf_revisions::acf_validate_post_id)可能已有人在做 @faisalahammad 于 30 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 78/100
AdvancedCustomFields/acf#1041 · 1 条评论 ·
-
True / False ACF control bug on RTL layouts可能已有人在做 @elicohenator 于 53 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 86/100
AdvancedCustomFields/acf#1038 ·
-
难度 2/5 1-3 小时 新手友好度 78/100
AdvancedCustomFields/acf#1034 · 3 条评论 ·
查看 AdvancedCustomFields/acf 的全部 Issue
相似的 Issue
-
bug
难度 2/5 1-3 小时 新手友好度 78/100
-
难度 2/5 1-3 小时 新手友好度 84/100
维护者通常 3 天内回复
-
难度 1/5 1 小时以内 新手友好度 91/100
benjaminkott/bootstrap_package#1675 ·
-
sync-en
难度 2/5 1-3 小时 新手友好度 74/100
维护者通常 1 天内回复
-
[Sync EN] Formatting improvements to "Large Objects (LOBs)" docs (#6056)可能已有人在做 关联的 PR 仍在进行中或已合并。 未关闭sync-en
难度 1/5 1 小时以内 新手友好度 74/100
维护者通常 1 天内回复