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

马丁·沃尔夫森(Maarten Wolfsen)

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

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

术语:ACF代表WordPress中的Advanced Custom Fields插件。

Jitendra角

您也可以使用插件来执行此操作,但是请使用以下代码来避免插件。

您需要在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/

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

将结果添加到WordPress搜索结果中

来自分类Dev

ACF邮政对象字段添加到WordPress管理列

来自分类Dev

将PHP页面添加到wordpress

来自分类Dev

将内容添加到 wordpress 页面

来自分类Dev

将预设添加到ACF位置字段(Google地图)

来自分类Dev

将预设添加到ACF位置字段(Google地图)

来自分类Dev

尝试使用jQuery将占位符属性添加到Wordpress中插件的搜索字段中

来自分类Dev

将datepicker字段添加到Wordpress中的插件设置页面

来自分类Dev

将datepicker字段添加到Wordpress中的插件设置页面

来自分类Dev

将搜索结果添加到Python中的表中

来自分类Dev

将Facebook页面搜索目录添加到网站

来自分类Dev

将Excel的SUM()添加到XLST结果页面?

来自分类Dev

将字段添加到AbstractUser管理页面

来自分类Dev

Wordpress ACF:如何通过自定义代码(PHP)将行添加到附加到用户的转发器字段中

来自分类Dev

WordPress重写仅将基本前缀添加到页面

来自分类Dev

将WooCommerce挂钩添加到Wordpress页面

来自分类Dev

将小部件添加到订单页面wordpress中

来自分类Dev

wordpress 将 html 添加到页面的最顶部

来自分类Dev

将隐藏字段添加到wordpress注册

来自分类Dev

WordPress ACF:将默认行添加到“管理”部分的“ Repeater”字段类型中,以获取自定义帖子类型

来自分类Dev

如何以编程方式将ACF组添加到Wordpress的后端?

来自分类Dev

将选项页ACF数据添加到Functions.PHP中的Wordpress简码中

来自分类Dev

添加到WordPress注册页面

来自分类Dev

如何仅将搜索表单添加到商店页面和类别页面?

来自分类Dev

将addClass添加到wrapAll结果

来自分类Dev

将列添加到MySQL结果

来自分类Dev

sql将列添加到结果

来自分类Dev

将池结果添加到字典

来自分类Dev

如何将选择列添加到显示搜索结果的JTable中

Related 相关文章

  1. 1

    将结果添加到WordPress搜索结果中

  2. 2

    ACF邮政对象字段添加到WordPress管理列

  3. 3

    将PHP页面添加到wordpress

  4. 4

    将内容添加到 wordpress 页面

  5. 5

    将预设添加到ACF位置字段(Google地图)

  6. 6

    将预设添加到ACF位置字段(Google地图)

  7. 7

    尝试使用jQuery将占位符属性添加到Wordpress中插件的搜索字段中

  8. 8

    将datepicker字段添加到Wordpress中的插件设置页面

  9. 9

    将datepicker字段添加到Wordpress中的插件设置页面

  10. 10

    将搜索结果添加到Python中的表中

  11. 11

    将Facebook页面搜索目录添加到网站

  12. 12

    将Excel的SUM()添加到XLST结果页面?

  13. 13

    将字段添加到AbstractUser管理页面

  14. 14

    Wordpress ACF:如何通过自定义代码(PHP)将行添加到附加到用户的转发器字段中

  15. 15

    WordPress重写仅将基本前缀添加到页面

  16. 16

    将WooCommerce挂钩添加到Wordpress页面

  17. 17

    将小部件添加到订单页面wordpress中

  18. 18

    wordpress 将 html 添加到页面的最顶部

  19. 19

    将隐藏字段添加到wordpress注册

  20. 20

    WordPress ACF:将默认行添加到“管理”部分的“ Repeater”字段类型中,以获取自定义帖子类型

  21. 21

    如何以编程方式将ACF组添加到Wordpress的后端?

  22. 22

    将选项页ACF数据添加到Functions.PHP中的Wordpress简码中

  23. 23

    添加到WordPress注册页面

  24. 24

    如何仅将搜索表单添加到商店页面和类别页面?

  25. 25

    将addClass添加到wrapAll结果

  26. 26

    将列添加到MySQL结果

  27. 27

    sql将列添加到结果

  28. 28

    将池结果添加到字典

  29. 29

    如何将选择列添加到显示搜索结果的JTable中

热门标签

归档