php - 将 ACF 字段添加到搜索结果页面 WordPress

标签 php wordpress advanced-custom-fields

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

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

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

最佳答案

您也可以使用插件来做到这一点,但使用下面的代码来避免插件。

您需要在 function.php 文件中添加此代码

<?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 );

供引用 - https://gist.github.com/charleslouis/5924863

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

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

关于php - 将 ACF 字段添加到搜索结果页面 WordPress,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36787391/

相关文章:

php - jQuery 选项卡的问题

php - 如何在 View / Blade 中显示两个表中的数据

php - 从 iis7 中的 wordpress URL 中删除 index.php

html - 如何在wordpress wp_nav函数中包装子菜单

php - 恢复sql语句的函数

css - 不同颜色的重复元素符号点

mysql - ACF 按转发器字段的子字段过滤自定义帖子

wordpress - 转发器字段的 ACF 查询帖子不为空

mysql - Wordpress ACF 元值显示在管理和前端中,但不会显示在数据库中,直到我在管理中单击更新

php - 每行水平滚动 div