Home Forums Backend Issues (wp-admin) How do you access a User ACF field from a Post Query?


How do you access a User ACF field from a Post Query?

  • I added a Company field to my users. Now I want to use that field as part of my Elementor Post Query. I have tried code like this: $vari = get_field(‘company_name1’, $author_id); to get the company name but it does not return anything. If I can get the data I will then set the Tag as that data. Am I missing something simple?

  • You cannot use data like this in elementor.

    You are trying to get the value of something based on the value of something else. As far as I know you cannot put PHP code into an elementor field. This requires creating a shortcode.

    An example shortcode based on the above

    add_shortcode('my_shortcode_name', 'my_shortcode_name_callback');
    my_shortcode_name_callback($atts=array, $content='') {
      global $post();
      $user = get_field('user_field', $post->ID);
      $user_content = get_field('company_name', $user);
      return $user_content;
  • To Clarify: The Elementor Post widget has a Post Query field that is the name of a PHP code snippet. This is where the code you talked about goes. I am able to pull the value of the Bio/Description field in the User profile that is signed in. I then set the Post Tag as being the value that is in the Bio field. Bio field has the company name. The Company name is also in the Tag of specific posts. This works perfectly. My challenge is that I can not access any of the ACF fields tied to the User Profile. I there a way to get to those fields?

  • Addition, your code mentions the Post ID. The ACF fields are not in a Post, they are linked to User Profiles. Hope that helps.

  • I really don’t know much about elementor.

    I cannot find a “Post” widget, but I can find a “Posts” widget.

    I do see a Query section, but cannot find a way to add a PHP code snippet to this?

    The only way I can describe it is the code I have provided. Beyond this it is a question about Elementor, not a question about ACF.

    I will try to help you (becuase I am in the process of learning elementor) but you’re going to need to tell me exact steps get to and do what you attempting to do starting from the WP Dashboard.

  • Just an FYI, You are in the correct widget. Post Widget. The Query section, is asking for the name of a Code snippet that defines what posts will be included when the page is displayed. This php code is defined using the Code Snippet plugin. The query could be as easy as saying category_name, Dogs , This would be inside PHP. I can get the User ID or name or Bio but I can not Get the ACF fields linked to the user. It comes back blank.
    I got a work around but I would still like to figure out how to access thses User profile fields through the PHP querry code. They work with Dynamic fields in Elementor. If you look at the attached image, this code does not work, If I use the decription field in the $vari= line, that works. I must be missing something simple.

  • I must still be missing something because I have added the code snippets plugin but I do not see any way to add a code snippet in the query Query section.

    I’d really like to help you figure this out, or figure out that it is not possible. But that means that I first have to figure out how to add this code snippet into the query.

    Are you sure there isn’t another plugin that I need to add?

  • Did it say the code snippet name goes in the Custom Query Box?

  • Now that I’ve looked at it, no, this is not possible. This only defines the query for the posts, so you can define getting posts that are related to the current user.

    However the posts will not include fields that are attached to that user.

    Now if you want to filter post by the company name associated with the user then what yu would need to do in your code snippet is

    1) Do a user query for find all users associated with that company.
    2) Filter the posts by the user field looking for any user in the list of users returned by the first part.

    // get the current user id and then get the company name associated with user
    $user_id = get_current_user();
    $company = get_field('company_name', 'user_'.$user_id);
    // get a list of user IDs associated with the above company name
    $args = array(
      'meta_key' => 'company_name,
      'meta_value ' => $company,
      'fields' => 'ID'
    $users = new WP_User_query($args);

    I’m not sure how the posts you want to get relate to the list of user IDs

  • THank you for the effort. If I can get the Comapny_name ACF field value of the current signed in user. I would then set the Post query tag to be equal to the value of that field. Posts have tags with company names and the users also have a company_name ACF field with te company they work for. I will try the meta code you showed above.

  • If company name is a tag (taxonomy) then you should look at get_term_by(). Or simply set a taxonomy query to query posts in that tag.

Viewing 13 posts - 1 through 13 (of 13 total)

You must be logged in to reply to this topic.