0

I'm setting a site up that once a user is logged in they will only see the ACFs that the admin has filled out in the backend. I have the ACF populating on the page, that's the easy part. I'm in now of need and help on trying to get these fields searchable, but the results much only show that of the user that is logged in. further down ill explain what I'm using at the moment.

I have the ACF fields set up below and works. Regarding the search to show the ACF fields, I am using Relevanssi plugin to show these fields but I can't seem to now show only the results that are tied to the currently logged in user.

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

I have tried the link above in addition, however, I will try again.

$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 ?>

So if the user ID is 4 then only the ACF fields that are used with user id 4 results should show when searched. I expect to only see those fields on the search page for that user, not all users fields, as the content shouldn't be viewable for anyone other than the currently logged in user.

UPDATE

An update, Since the admin only gets to assign values in the user profile pages, every ACF is getting used under the ID of the admin (being 1) so every time you search it's dragging up every ID that uses 1 when actually I only need the ID to change according to user profile it matches

/* 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
            ) );
    }

I need work around that as admin can save the profile as if another user was saving can the IDs change in the usermeta for the search to correctly work

1 Answers1

0

Hello here's a solution that works for me;

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

Now $event is an event object from event manager. Notice that this could be $post variable as well

Hope that helps :)

bl3ck
  • 75
  • 9
  • Hi, those three fields are working fine and just test scenarios. If I have explained not well enough then my bad but I'm stuck on getting those fields ONLY for one being logged in into the search results. – henry puttick Jun 09 '19 at 11:07