You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在搜索结果中仅显示当前登录用户的ACF用户档案字段

Hey there, let's work through your problem step by step—we've got two key issues to fix: getting ACF fields saved to the correct target user instead of the admin, then configuring Relevanssi to only return results for the logged-in user.

1. Fix ACF Fields to Save to the Target User ID

The core problem here is that when your admin edits a user's profile, ACF is saving fields to the admin's own user ID (ID 1) instead of the user being edited. Here's how to fix that:

Option 1: Check ACF Field Group Location Rules

First, make sure your ACF field group is set up correctly to target user profiles:

  • Go to your ACF field group settings
  • Under Location Rules, set User Form → is equal to → All User Forms
  • This tells ACF to load the fields on user edit pages and automatically save them to the user being edited, not the current admin.

Option 2: Force ACF to Save to the Target User (If Location Rules Aren't Working)

If the above doesn't resolve it, add this code to your theme's functions.php or a custom plugin to override the save target:

// Ensure ACF saves fields to the user being edited, not the admin
add_filter('acf/save_post', 'force_acf_save_to_target_user', 10, 1);
function force_acf_save_to_target_user($post_id) {
    // Check if we're on a user edit page in the admin
    if (isset($_GET['user_id']) && is_admin()) {
        $target_user_id = intval($_GET['user_id']);
        if ($target_user_id > 0) {
            // Return the user's ACF identifier format (user_{ID})
            return 'user_' . $target_user_id;
        }
    }
    return $post_id;
}

This hook detects when you're editing a user profile and forces ACF to save fields to that specific user instead of the admin.

2. Configure Relevanssi to Only Show the Logged-In User's Results

Once your ACF fields are linked to the correct users, we need to restrict Relevanssi searches to only the current user's data.

Step 1: Index User ACF Fields in Relevanssi

First, make sure Relevanssi is indexing your user meta fields:

  • Go to Relevanssi → Settings → Indexing
  • Check the box for User meta
  • In the User meta fields to index field, add your ACF field names separated by commas: test, test2, test3
  • Save settings and re-index your site.

Step 2: Filter Relevanssi Results to the Current User

Add this code to filter search results so only the logged-in user's ACF fields appear:

// Filter Relevanssi hits to only include the current user's data
add_filter('relevanssi_hits_filter', 'filter_relevanssi_to_current_user');
function filter_relevanssi_to_current_user($hits) {
    // Skip filtering if no user is logged in
    if (!is_user_logged_in()) return $hits;

    $current_user_id = get_current_user_id();
    $filtered_hits = [];

    foreach ($hits[0] as $hit) {
        // Keep results that belong to the current user
        if ($hit->post_type === 'user' && $hit->ID === $current_user_id) {
            $filtered_hits[] = $hit;
        }
        // If you're also indexing posts linked to the user, uncomment this:
        // elseif ($hit->post_type === 'post' && $hit->post_author === $current_user_id) {
        //     $filtered_hits[] = $hit;
        // }
    }

    $hits[0] = $filtered_hits;
    return $hits;
}

Step 3: Optional: Restrict the Search Query to the Current User

For an extra layer of restriction, modify the Relevanssi SQL query to only search the current user's meta:

// Modify Relevanssi query to only target the current user's fields
add_filter('relevanssi_query_filter', 'restrict_relevanssi_query_to_current_user');
function restrict_relevanssi_query_to_current_user($query) {
    if (!is_user_logged_in()) return $query;

    $current_user_id = get_current_user_id();
    // Add condition to only search the current user's ACF meta
    $query .= " AND relevanssi.doc = 'user_" . $current_user_id . "'";

    return $query;
}
3. Fix Your Existing Display Code

Your current code has a couple of issues—here's the corrected version to display the logged-in user's fields:

$current_user_id = get_current_user_id();
// Use get_field() to return values (the_field() outputs directly)
$test = get_field('test', 'user_' . $current_user_id);
$test2 = get_field('test2', 'user_' . $current_user_id);
$test3 = get_field('test3', 'user_' . $current_user_id);

// Get attachment URLs correctly
$url = wp_get_attachment_url($test2);
$url2 = wp_get_attachment_url($test3);

// Display the values
echo $test;
echo $test2;
echo $test3;
Testing the Setup
  1. Verify ACF Save: Have your admin edit user ID 4's profile, fill in the ACF fields, then use get_field('test', 'user_4') to confirm the data is saved to user 4, not admin ID 1.
  2. Test Search: Log in as user 4, search for content from their ACF fields—you should only see results linked to user 4, no other users' data.

内容的提问来源于stack exchange,提问作者henry puttick

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 09:25:39