针对管理员的pre_get_post和搜索查询

时间:2020-05-23 作者:v3nt

我一直在努力在搜索结果中包含几个自定义字段。自定义字段通过ACF进行管理。

首先我想把这个给管理员/wp管理/编辑。php?post\\u类型=位置。

我不明白为什么这行不通。我尝试过它的不同版本和不同的订单。我也用数组遍历了posts\\u search filter路径,但就是无法实现这一点。

感谢您的帮助。

add_filter(\'pre_get_posts\', __NAMESPACE__ . \'\\\\search_custom_fields\');

function search_custom_fields($query)
{
if ($query->is_search && $query->is_main_query()) {
    // this works.
    echo "search_custom_fields:   " . $query->is_main_query() . " " . $query->is_search;
    echo "Setting meta_query in search_custom_fields. ";

    $search_word = get_query_var(\'s\');

    $meta_query = array(
        \'relation\' => \'OR\',
        array(
            \'key\'     => \'loc_refid\',
            \'value\'   => get_search_query(),
            \'compare\' => \'LIKE\',
        ), array(
            \'key\'     => \'location_postcode_preview\',
            \'value\'   => get_search_query(),
            \'compare\' => \'LIKE\',
        ),
    );

    // if i uncomment this no results appear?!
    // $query->set(\'meta_query\', $meta_query);

    // these work have an affect on the results
    $query->set(\'orderby\', \'date\');  
    $query->set(\'order\', \'ASC\');

}

return $query;
}

print\\r($query)给我-

 [query_vars]=>
        [meta_query] => Array
                (
                    [key] => loc_refid
                    [value] => MN0068
                    [compare] => LIKE
                )

 [meta_query] =>  
 [date_query] => 
 [post_count] => 0
 ...
提前谢谢。

2 个回复
最合适的回答,由SO网友:v3nt 整理而成

因此,事实证明,您实际上无法以这种方式将ACF/自定义字段添加到搜索查询中。这似乎迫使和进入了质疑。

我使用了多种来源的组合来创建下面的内容,现在效果非常好。

/* ADD META FIELD TO SEARCH QUERY */
// fields for Ajax search, admin and normal search
if (!isset($GLOBALS[\'current_screen\']) && !is_customize_preview()):
    // DAMN AJAX! is_admin() is always true when using it ;-/
    add_meta_field_to_search_query(\'loc_refid\');
        add_meta_field_to_search_query(\'location_postcode_preview\');
    add_meta_field_to_search_query(\'location_region\');
    add_meta_field_to_search_query(\'location_areas\');

elseif (is_admin()):
    add_meta_field_to_search_query(\'loc_refid\');
    add_meta_field_to_search_query(\'location_postcode\');
    add_meta_field_to_search_query(\'location_postcode_preview\');
    add_meta_field_to_search_query(\'location_region\');
    add_meta_field_to_search_query(\'location_areas\');
    add_meta_field_to_search_query(\'location_map_position\' );
    add_meta_field_to_search_query(\'user_organisation\');
    add_meta_field_to_search_query(\'user_position\');
    add_meta_field_to_search_query(\'client_location\');
    add_meta_field_to_search_query(\'user_email\');
else:
    add_meta_field_to_search_query(\'loc_refid\');           
    add_meta_field_to_search_query(\'location_postcode_preview\');
    add_meta_field_to_search_query(\'location_region\');
    add_meta_field_to_search_query(\'location_areas\');

endif;

function add_meta_field_to_search_query($field, $condition = "LIKE")
{
    // echo "add_meta_field_to_search_query; field:$field";

    if (isset($GLOBALS[\'added_meta_field_to_search_query\'])) {
        $GLOBALS[\'added_meta_field_to_search_query\'][] = \'\\\'\' . $field . \'\\\'\';
        return;
    }

    $GLOBALS[\'added_meta_field_to_search_query\']   = array();
    $GLOBALS[\'added_meta_field_to_search_query\'][] = \'\\\'\' . $field . \'\\\'\';

    add_filter(\'posts_join\', function ($join) {
        // echo "$s \'posts_join\' <br />";
        global $wpdb;
        if (is_search()) {
            $join .= " LEFT JOIN $wpdb->postmeta ON $wpdb->posts.ID = $wpdb->postmeta.post_id ";
        }
        return $join;
    });

    add_filter(\'posts_groupby\', function ($groupby) {
        // echo "$s \'posts_groupby\' <br />";
        global $wpdb;
        if (is_search()) {
            $groupby = "$wpdb->posts.ID";
        }
        return $groupby;
    });

    add_filter(\'posts_search\', function ($search_sql) {
        global $wpdb;

        $search_terms = get_query_var(\'search_terms\');

        if (!empty($search_terms)) {
            // echo "posts_search<br />";
            // print_r($search_terms);
            foreach ($search_terms as $search_term) {
                // echo "search_term: $search_term<br />";
                $old_or     = "OR ({$wpdb->posts}.post_content LIKE \'{$wpdb->placeholder_escape()}{$search_term}{$wpdb->placeholder_escape()}\')";
                $new_or     = $old_or . " OR ({$wpdb->postmeta}.meta_value LIKE \'{$wpdb->placeholder_escape()}{$search_term}{$wpdb->placeholder_escape()}\' AND {$wpdb->postmeta}.meta_key IN (" . implode(\', \', $GLOBALS[\'added_meta_field_to_search_query\']) . "))";
                $search_sql = str_replace($old_or, $new_or, $search_sql);
            }
        }

        $search_sql = str_replace(" ORDER BY ", " GROUP BY $wpdb->posts.ID ORDER BY ", $search_sql);

        return $search_sql;
    });

    // ajax seach only, basically same as above but checking for $GLOBALS[\'fd_search_term\'] which holds the search string
    add_filter(\'posts_where\', function ($search_sql) {
        global $wpdb;

        // print_r($wpdb);
        $search_terms = explode(\' \', $GLOBALS[\'fd_search_term\']);

        // if (!empty($search_terms) && !isset($GLOBALS[\'current_screen\']) && !is_customize_preview() && !is_search()) {

        if ($GLOBALS[\'fd_search_term\']) {
            // echo "AJAX posts_search<br />";
            // print_r($search_terms);
            foreach ($search_terms as $search_term) {
                // echo "search_term: $search_term<br />";
                $old_or     = "OR ({$wpdb->posts}.post_content LIKE \'{$wpdb->placeholder_escape()}{$search_term}{$wpdb->placeholder_escape()}\')";
                $new_or     = $old_or . " OR ({$wpdb->postmeta}.meta_value LIKE \'{$wpdb->placeholder_escape()}{$search_term}{$wpdb->placeholder_escape()}\' AND {$wpdb->postmeta}.meta_key IN (" . implode(\', \', $GLOBALS[\'added_meta_field_to_search_query\']) . "))";
                $search_sql = str_replace($old_or, $new_or, $search_sql);
            }
        }

        $search_sql = str_replace(" ORDER BY ", " GROUP BY $wpdb->posts.ID ORDER BY ", $search_sql);

        return $search_sql;
    });

}

SO网友:shanebp

你试过了吗add_action 而不是add_filter ?

add_action(\'pre_get_posts\', \'search_custom_fields\');
是的public 您的post\\u类型是否设置为true?

要在admin中使用,请尝试:

if ( is_admin() && $query->is_main_query() ) {
    if ( $query->is_search ) {
       //etc.

相关推荐

Get_Search_Query()不能处理带有“与”号的搜索查询

我正在使用简单的代码来创建搜索页面$argscontactdirectory = array( \"s\" => get_search_query(), \'post_type\' => array( \'expertarticle\'), \'orderby\' => \'title\', \'order\' => \'ASC\',