将 ACF 字段添加到搜索结果页面 WordPress [英] Add ACF fields to search results page WordPress

查看:35
本文介绍了将 ACF 字段添加到搜索结果页面 WordPress的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 search.php 的文件,其中添加了所有搜索结果.搜索表单位于主页上.问题是,我没有搜索结果的特殊页面,但我想向此页面添加 ACF 字段.我在 ACF 插件的下拉菜单位置"中进行了搜索,但找不到要添加组的 search.php 文件.如果我的解释有点模糊,我很抱歉,但我不知道如何准确地解释这一点.

总结,我想将 ACF 字段添加到我的 search.php 搜索结果页面.

术语:ACF 代表 WordPress 中的高级自定义字段插件.

解决方案

您也可以使用插件来完成,但请使用以下代码来避免使用插件.

你需要在你的function.php文件中加入这段代码

query_vars['s'];//分解搜索表达式以获取搜索词$exploded = expand(' ', $terms );if( $exploded === FALSE || count( $exploded ) == 0 )$exploded = array( 0 => $terms );//重置搜索以按照我们的意愿重建它$where = '';//获取 searcheable_acf,您要在其中搜索内容的高级自定义字段列表$list_searcheable_acf = list_searcheable_acf();foreach( $exploded as $tag ) :$where .= "和 ((wp_posts.post_title LIKE '%$tag%')或 (wp_posts.post_content LIKE '%$tag%')或存在 (SELECT * FROM wp_postmeta哪里 post_id = wp_posts.ID和 (";foreach ($list_searcheable_acf 作为 $searcheable_acf) :如果($searcheable_acf == $list_searcheable_acf[0]):$where .= " (meta_key LIKE '%" . $searcheable_acf . "%' AND meta_value LIKE '%$tag%') ";别的 :$where .= " OR (meta_key LIKE '%" . $searcheable_acf . "%' AND meta_value LIKE '%$tag%') ";万一;Endforeach;$where .= "))或存在 (SELECT * FROM wp_comments哪里comment_post_ID = wp_posts.IDAND comment_content LIKE '%$tag%')或存在 (SELECT * FROM wp_terms内连接 wp_term_taxonomyON wp_term_taxonomy.term_id = wp_terms.term_id内连接 wp_term_relationshipsON wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id在哪里 (分类 = 'post_tag'OR 分类法 = '类别'或分类法 = 'myCustomTax')AND object_id = wp_posts.IDAND wp_terms.name LIKE '%$tag%'))";Endforeach;返回 $where;}add_filter( 'posts_search', 'advanced_custom_search', 500, 2);

供参考 - https://gist.github.com/charleslouis/5924863

如果第一个不起作用,请尝试此解决方案.

https://support.advancedcustomfields.com/forums/topic/making-customfields-searchable/

I have a file named search.php, where all the search results are added. The search form is located on the homepage. The problem is, I have no special page for the search results, but I want to add ACF fields to this page. I have searched in the dropdown menu 'location' in the ACF plugin, but I can't find the search.php file to add groups to. I am sorry if my explaination is a bit vague, but I don't know how to explain this exactly.

Summary, I want to add ACF fields to my search.php search results page.

Terminology: ACF stand for the Advanced Custom Fields plugin in WordPress.

解决方案

You can also do it using plugin but use below code to avoide plugin.

You need to add this code in you function.php file

<?php
/**
 * [list_searcheable_acf list all the custom fields we want to include in our search query]
 * @return [array] [list of custom fields]
 */
function list_searcheable_acf(){
  $list_searcheable_acf = array("title", "sub_title", "excerpt_short", "excerpt_long", "xyz", "myACF");
  return $list_searcheable_acf;
}
/**
 * [advanced_custom_search search that encompasses ACF/advanced custom fields and taxonomies and split expression before request]
 * @param  [query-part/string]      $where    [the initial "where" part of the search query]
 * @param  [object]                 $wp_query []
 * @return [query-part/string]      $where    [the "where" part of the search query as we customized]
 * see https://vzurczak.wordpress.com/2013/06/15/extend-the-default-wordpress-search/
 * credits to Vincent Zurczak for the base query structure/spliting tags section
 */
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 );

for reference - https://gist.github.com/charleslouis/5924863

Try This solution if first is not working.

https://support.advancedcustomfields.com/forums/topic/making-customfields-searchable/

这篇关于将 ACF 字段添加到搜索结果页面 WordPress的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆