簡體   English   中英

添加 ACF 字段到搜索結果頁面 WordPress

[英]Add ACF fields to search results page WordPress

我有一個名為 search.php 的文件,其中添加了所有搜索結果。 搜索表單位於主頁上。 問題是,我沒有專門的搜索結果頁面,但我想在這個頁面上添加 ACF 字段。 我在 ACF 插件的下拉菜單“位置”中進行了搜索,但找不到要將組添加到的 search.php 文件。 如果我的解釋有點含糊,我很抱歉,但我不知道如何准確解釋。

總結一下,我想將ACF字段添加到我的search.php搜索結果頁面。

術語:ACF 代表 WordPress 中的 Advanced Custom Fields 插件。

你也可以使用插件來做,但使用下面的代碼來避免插件。

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

這個對我有用

function custom_search_query( $query ) {
    if ( !is_admin() && $query->is_search ) {
        $result = $query->query_vars['s'];
        $query->query_vars['s'] = '';
        $query->set('meta_query', array('relation' => 'OR',
            array(
                'key'     => 'acf_name', // ACF FIELD NAME OR POST META
                'value'   => $result,
                'compare' => 'LIKE',
            )
        ));
        $query->set('post_type', 'post'); // optional POST TYPE
    }
}
add_filter( 'pre_get_posts', 'custom_search_query');

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM