如何使用条件逻辑搜索ACF(高级自定义字段)数据?

时间:2016-02-24 16:43:44

标签: wordpress advanced-custom-fields

所有

我正在使用Wordpress进行搜索,并且它正在返回ACF数据。问题是,对于使用条件逻辑隐藏的字段,它们仍然在搜索结果中返回。

有没有人碰巧知道如何排除隐藏的字段?或者,是否有人知道这些项目被声明为隐藏在数据库中?我似乎无法在wp_postmeta中找到任何将字段与另一个字段联系起来的内容。

编辑:按要求添加了一些代码

<?php
error_reporting(0);
global $query_string;
$query_args = explode("&", $query_string);
$search_query = array();

foreach($query_args as $key => $string) {
    $query_split = explode("=", $string);
    $search_query[$query_split[0]] = urldecode($query_split[1]);
}
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$search = new WP_Query(
        $search_query,
        array( 
            'post_type' => 'page',
            'post_status'=>'publish', 
            'post__in' => array(),
            //'posts_per_page' => -1,
            'paged' => $paged 
        )
    );
$resultArray = array();
    if (have_posts()){
        while(have_posts()): the_post();
            //places the WP post meta data into an array
            $array  = get_post_meta($post->ID);
            //Flattens the array into one-dimensonal
            $result = call_user_func_array('array_merge', $array);
            //changes the search term to all lower case
            $searchword = strtolower($search_query['s']);
            //Searches the $result array for the $searchword and saves all occurances into $matches array
            $matches = array_filter($result, function($var) use ($searchword) { return preg_match("/\b$searchword\b/i", strip_tags($var)); });
            //Grab the first value of the $matches array since we won't always know the key
            $first_value = reset($matches);

            if(!empty($first_value)){
                array_push($resultArray,
                    array(
                        'id'=>$post->ID,
                        'match' =>strip_tags($first_value)
                    )
                );
            }
        endwhile;
    }
   $total_results = count($resultArray); 
?>

0 个答案:

没有答案