在wordpress中匹配搜索结果后识别ACF字段
Posted
技术标签:
【中文标题】在wordpress中匹配搜索结果后识别ACF字段【英文标题】:Identifying ACF field after a search result is matched in wordpress 【发布时间】:2019-02-16 01:44:13 【问题描述】:是否可以获取搜索字符串匹配的 ACF 字段名称/ID? ACF 字段也包含在 wordpress 的默认搜索功能中。因此,当搜索字符串与 ACF 字段值匹配时,我还想识别字段名称/ID。这可能吗?
【问题讨论】:
你的意思是搜索文本如果与 acf 字段匹配,那么你想得到那个 acf 字段的详细信息吗? 是的,完全正确 :) 抱歉我的错误解释 【参考方案1】:你可以试试这个代码脚本。我希望这对你有用。
$fields = get_fields(get_the_ID());
$acfField = array();
$search_query = get_search_query();
foreach( $fields as $name => $value ):
if($search_query == $value)
$acfField['name'] = $name;
$acfField['value'] =
$value; break;
endforeach;
print_r($acfField);
把它放在search.php
的循环中:
while ( have_posts() ) : the_post();
$fields = get_fields(get_the_ID());
$acfField = array();
$search_query = get_search_query();
foreach( $fields as $name => $value ):
if($search_query == $value)
$acfField['name'] = $name;
$acfField['value'] = $value;
break;
endforeach;
print_r($acfField);
get_template_part( 'template-parts/post/content' );
endwhile;
注意:实际上,我们无法获取所有的acf 字段。我们只能使用get_fields(get_the_ID());
通过帖子ID获取特定帖子的所有ACF字段
【讨论】:
感谢您的快速帮助。我很快就会试试这个并告诉你。 好,你可以根据自己的要求使用。【参考方案2】:你需要做的就是将这行添加到function.php
<?php
function list_searcheable_acf()
$list_searcheable_acf = array("title", "sub_title", "excerpt_short", "excerpt_long", "xyz", "myACF");
return $list_searcheable_acf;
function advanced_custom_search( $where, &$wp_query )
global $wpdb;
if ( empty( $where ))
return $where;
// get search expression
$terms = $wp_query->query_vars[ 's' ];
// explode search expression to get search terms
$exploded = explode( ' ', $terms );
if( $exploded === FALSE || count( $exploded ) == 0 )
$exploded = array( 0 => $terms );
// reset search in order to rebuilt it as we whish
$where = '';
// get searcheable_acf, a list of advanced custom fields you want to search content in
$list_searcheable_acf = list_searcheable_acf();
foreach( $exploded as $tag ) :
$where .= "
AND (
(wp_posts.post_title LIKE '%$tag%')
OR (wp_posts.post_content LIKE '%$tag%')
OR EXISTS (
SELECT * FROM wp_postmeta
WHERE post_id = wp_posts.ID
AND (";
foreach ($list_searcheable_acf as $searcheable_acf) :
if ($searcheable_acf == $list_searcheable_acf[0]):
$where .= " (meta_key LIKE '%" . $searcheable_acf . "%' AND meta_value LIKE '%$tag%') ";
else :
$where .= " OR (meta_key LIKE '%" . $searcheable_acf . "%' AND meta_value LIKE '%$tag%') ";
endif;
endforeach;
$where .= ")
)
OR EXISTS (
SELECT * FROM wp_comments
WHERE comment_post_ID = wp_posts.ID
AND comment_content LIKE '%$tag%'
)
OR EXISTS (
SELECT * FROM wp_terms
INNER JOIN wp_term_taxonomy
ON wp_term_taxonomy.term_id = wp_terms.term_id
INNER JOIN wp_term_relationships
ON wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
WHERE (
taxonomy = 'post_tag'
OR taxonomy = 'category'
OR taxonomy = 'myCustomTax'
)
AND object_id = wp_posts.ID
AND wp_terms.name LIKE '%$tag%'
)
)";
endforeach;
return $where;
add_filter( 'posts_search', 'advanced_custom_search', 500, 2 );
【讨论】:
我的缩进出错了,请复制完整代码以上是关于在wordpress中匹配搜索结果后识别ACF字段的主要内容,如果未能解决你的问题,请参考以下文章
php WordPress:在ACF字段中搜索,functions.php
php 搜索自定义字段:在Wordpress搜索中包括自定义字段/ ACF
php PHP - Wordpress - 搜索 - wordpress自定义搜索功能,包含ACF /高级自定义字段和分类法以及拆分表达式