【问题标题】:How to show ACF fields from users profile in search results that are only assigned to current users ID如何在仅分配给当前用户 ID 的搜索结果中显示来自用户配置文件的 ACF 字段
【发布时间】:2019-10-24 01:47:06
【问题描述】:

我正在设置一个站点,一旦用户登录,他们将只能看到管理员在后端填写的 ACF。我在页面上填充了 ACF,这很容易。我现在需要帮助并尝试使这些字段可搜索,但结果大多只显示已登录用户的信息。进一步解释我目前正在使用的内容。

我在下面设置了 ACF 字段并且可以正常工作。关于显示 ACF 字段的搜索,我正在使用 Relevanssi 插件来显示这些字段,但我现在似乎不能只显示与当前登录用户相关的结果。

https://stackoverflow.com/questions/36787391/add-acf-fields-to-search-results-page-wordpress 

我已经尝试过上面的链接,但是我会再试一次。

$author_id = get_the_author_meta('ID');
$test  = the_field('test', 'user_' . $author_id);
$test2 = the_field('test2', 'user_' . $author_id);
$test3 = the_field('test3', 'user_' . $author_id);

$url = wp_get_attachment_url( $current_user->test2 );
$url2 = wp_get_attachment_url( $current_user->test3 );

echo $current_user->test 
echo $current_user->test2 
echo $current_user->test3 ?>

因此,如果用户 ID 为 4,则只有与用户 ID 4 一起使用的 ACF 字段才会在搜索时显示。我希望仅在该用户的搜索页面上看到这些字段,而不是所有用户字段,因为除了当前登录的用户之外的任何人都不应查看内容。

更新

更新,由于管理员只能在用户个人资料页面中分配值,因此每个 ACF 都在管理员的 ID(为 1)下使用,因此每次搜索时,它都会拖动实际上使用 1 的每个 ID我只需要根据匹配的用户配置文件更改 ID

/* Replace with custom Author meta box */
function wpse39084_custom_author_metabox() {  
    add_meta_box( 'authordiv', __('Author'), 'wpse39084_custom_author_metabox_insdes','post');  
 } 
add_action( 'add_meta_boxes', 'wpse39084_custom_author_metabox');  


    /* Include all users in post author dropdown*/
    /* Mimics the default metabox http://core.trac.wordpress.org/browser/trunk/wp-admin/includes/meta-boxes.php#L514 */
    function wpse39084_custom_author_metabox_insdes() {
          global $user_ID;
          global $post;
          ?>
          <label class="screen-reader-text" for="post_author_override"><?php _e('Author'); ?></label>

          <?php
            wp_dropdown_users( array(
                 'name' => 'post_author_override',
                 'selected' => empty($post->ID) ? $user_ID : $post->post_author,
                 'include_selected' => true
            ) );
    }

我需要解决这个问题,因为管理员可以保存配置文件,就像另一个用户正在保存一样,可以更改 usermeta 中的 ID 以使搜索正常工作

【问题讨论】:

    标签: php wordpress search


    【解决方案1】:

    您好,这是一个适合我的解决方案;

    $author_id = get_post_field( 'post_author', $event->ID );
    

    现在 $event 是来自事件管理器的事件对象。请注意,这也可能是 $post 变量

    希望有帮助:)

    【讨论】:

    • 嗨,这三个字段工作正常,只是测试场景。如果我解释得不够好,那我就错了,但我坚持只为登录搜索结果的人获取这些字段。
    猜你喜欢
    • 1970-01-01
    • 2014-01-26
    • 1970-01-01
    • 1970-01-01
    • 2021-10-25
    • 1970-01-01
    • 2021-04-03
    • 2019-02-16
    • 1970-01-01
    相关资源
    最近更新 更多