Display CPT loop via ACF Relationship field in Author Archive

I have a user profile for each of my partners. It has an ACF field group assigned, and I’m using beaver themer to display. The issue is, I can’t get a loop of related projects (CPT) to show from a relationship field.

This template works beautifully on a CPT post, but not the user profile, author archive.


<Loop acf_relationship=client_projects>
  <If check="{Taxonomy stage}{Term title}{/Taxonomy}" value=active>
    <div class="row">
      <div class="col-1"><Field acf_editor=project_pid /></div>
      <div class="col-4"><Field acf_editor=project_name /></div>
      <div class="col-2"><Taxonomy stage><Term title /></Taxonomy></div>
      <div class="col-1"><Field acf_editor=project_start /></div>
      <div class="col-1"><Field acf_editor=project_target /></div>
      <div class="col-3"><Field acf_editor=project_next_step /></div>
    </div> 
  </If>
</Loop>

I understand that custom post types don’t show on an author archives by default, and I tried a get-pre-post snippet with no avail. Is there a way to do this?

If it’s too much of a thing, is there a way to show only their projects?

Welcome to the forum, Andrew! It should definitely be possible to do what you’re trying to do.

The fact that this seems to work “on a CPT post, but not the user profile” is probably our biggest hint. Three questions for you:

  1. What’s the slug/name of your custom post type?
  2. What is the slug/name of your ACF relationship field?
  3. In the “location rules” for your field group, what have you set as the location(s) where this field should be added?

@captaindrew80 Let me know if you can provide the info mentioned in the reply above. I think it should be pretty easy to troubleshoot this, I just want to make sure I have all the info to make sure my suggestions make sense.

  1. client_projects

Sorry for the long delay and thanks for following up:

  1. What’s the slug/name of your custom post type? projects
  2. What is the slug/name of your ACF relationship field? client_projects
  3. In the “location rules” for your field group, what have you set as the location(s) where this field should be added? - User form = All

Got it. So your client_projects field seems to exists as user meta. What your template is doing with that first line, <Loop acf_relationship=client_projects> is it’s looking at the current post (whether that’s the current projects post on a singular post page or the current user on a user profile) and looping through the ACF relationship field called client_projects on the current post. But that field doesn’t exist on all your post types, it sounds like it only exists on your user meta, which means that the data would only be available within a user loop (a loop of multiple users or a single user). So that might explain why you’re not able to just move that template around and have it work in all the places. Your statement that the template “works beautifully on a CPT post, but not the user profile” is a bit confusing to me because if you do have the location rule for the field set to “User form = All”, then the field should only exist in the context of a user’s profile, not in the context of your projects CPT.

In any case, the solution here is probably going to be to first tell L&L where to look to find that relationship field, and then loop through the field. If the field relates to the current user, then you might want to do something like this:

<Loop type=user id=current>
  <Loop acf_relationship=client_projects>
    <Field title /> <!--add in the data you want to display here-->
  </Loop>
</Loop>

Let me know if that works for you. If not, try writing this template and taking a look at the output to confirm whether the client_projects exists as a user field.

<Loop type=user id=current>
  <Field all />
</Loop>

You might want to check out this blog post, specifically the section called “How does a loop affect the context of other tags?”

Thanks for the reply. I got this working (i was using the wrong field id) but I just need to know how to display multiple taxonomies or exclude multiple taxonomies in the “if check” or by using something else. For instance, I want to display all projects that don’t have a pm_status of Complete or Archived.

Also, I remember using ID=this instead of ID=current in custom content shortcode (RIP) to show the projects for that archive page, rather than the logged in user. Is that possible here?

This should help you with that. If you want to only display posts that have multiple taxonomy terms applied, you can pass them as an array to the include parameter.

I think you’ll have to provide more detail about what you’re trying to do here. From my understanding of your original post, you have an ACF relationship custom field associated with your users, so to loop through the fields associated with the current user, you needed to use <Loop type=user id=current>. If you want to display data from an archive page or create an archive page with some data, that’s probably a totally different thing and I’m not sure where/if id=current would come into play. If you can explain where the data exists in the back end, how you want it to display on the front end, and what you’ve tried to achieve that, I could help steer you in the right direction.


Edit: thinking about this a bit more, if you’re saying you have fields on a page and you want to put an L&L template on that page to display fields from the current page, you wouldn’t need to wrap it in anything. If you don’t tell L&L where to look to find data, it’ll use the “default context” of the page and display fields from the current page. So if you had an ACF relationship field on the current page, you could just write:

<Loop acf_relationship=your_field_name_here>
    <Field title /> <!--add in the data you want to display here-->
</Loop>

If you want to display a regular text field or something like that from the current page, you can just write <Field your_field_name /> right in the base of your template.

Thanks again for keeping with this.

Here’s where I’m at now using the code below with user=current.

<Loop type=user id=current> <Loop acf_relationship=client_projects> <Field title /> <!--add in the data you want to display here--> </Loop> </Loop>

Nothing shows at all when I use this:

<Loop acf_relationship=your_field_name_here> <Field title /> <!--add in the data you want to display here--> </Loop>

After playing with this for awhile, I’m realizing that I’m not being clear as to the goal.

The goal is to go to (for instance) Arun’s profile (author archive) and see projects that he’s the author of. Querying the ACF relationship field isn’t necessary. It was originally a workaround for something else.

I know that the author archive doesn’t natively show custom post types, so I added this snippet:

    function post_types_author_archives($query) {
        if ($query->is_author)
                // Add 'books' CPT and the default 'posts' to display in author's archive
                $query->set( 'post_type', array('projects', 'requests', 'post') );
        remove_action( 'pre_get_posts', 'custom_post_author_archive' );
    }
    add_action('pre_get_posts', 'post_types_author_archives');

So if I can at least get the user ID of the current author archive, and then show the projects with that ID, I’m golden. So maybe step 1) get projects 2) filter by the user id of the current author archive?

Aside, Beaver Themer module can query the ACF field relationship field, but it can’t test for the taxonomy to filter. This is why I’m going with your plugin.

Ooooh! That makes sense! I was thinking that you wanted a user to be able to log in and see some fields that were associated with their own user. That’s why I suggested <Loop type=user id=current>, which creates a loop of the currently logged-in user and allows you to show fields associated with that user. That’s why when you’re logged in, it’s showing you the fields associated with your user.

Instead, you’re on an author archive and you want to display fields from that author.

I believe when you’re on an author archive (which is, by default, a post archive that only shows posts written by that author), you can display any of the fields associated with a post loop. So in your case, you could use <Field author /> to display the author’s id or <Field author_name /> to display the author’s username. You could then pass that information to your user loop so that instead of displaying data about the currently logged-in user, you’d display data from whoever’s author archive you’re currently looking at.

In practice, I think that’d be something like this:

<Loop type=user id="{Field author}">
  <Loop acf_relationship=client_projects>
    <Field title /> <!--add in the data you want to display here-->
  </Loop>
</Loop>

Let me know if that makes sense!

I don’t think you’d necessarily need that snippet you shared, since the L&L markup should be what’s grabbing the projects data from your client_projects field so you wouldn’t also need the page query to be grabbing that as well if you’re not using the page’s query to display the data. Not a big issue, it just might be redundant so I thought I’d mention that this would probably still work even if you got rid of the snippet.

A smart lurker who saw my reply above just pointed out to me that this approach wouldn’t work on the author archive of a person who hadn’t authored any posts, which makes sense and isn’t something I had considered when I made the suggestion above. My idea above relies on there being at least one post in the archive that you could use to grab data from the author field.

I’m told there’s a fancy way to get data about the author when you’re on an author archive with the archive_author field. You can get data about the author by looping through that field. Here’s what it would look like to loop through that field to get the user ID and then set that as a variable for later use:

<Loop field=archive_author>
  <Set user_id><Field id /></Set>
</Loop>

Then once you have that ID saved as a variable, the rest of my earlier suggested markup should work properly when you pass the user_id variable to the id parameter:

<Loop type=user id="{Get user_id}">
  <Loop acf_relationship=client_projects>
    <Field title /> <!--add in the data you want to display here-->
  </Loop>
</Loop>

If you wanted to do this all in one go without setting a variable, you could also write something like this:

<Loop type=user id="{Loop field=archive_author}{Field id}{/Loop}">
  <Loop acf_relationship=client_projects>
    <Field title /> <!--add in the data you want to display here-->
  </Loop>
</Loop>
1 Like

This worked! And it’s filtering the taxonomy too. Thank you!

<Loop field=archive_author>
  <Set user_id><Field id /></Set>
</Loop>

<Loop type=user id="{Get user_id}" >

 <Loop acf_relationship=request_partner_v2 >
  <If loop type=taxonomy_term taxonomy=pm_status include=planned,on-deck,active,qa,revision,hold post=current>

   <div class="row">
      <div class="col-1">
        <Taxonomy pm_status><Term title /></Taxonomy>
        </div>
      <div class="col-1">
        <Field acf_editor=partner_request_type />
      </div>
      <div class="col-lg-3">
        <a href="{Field url}" target="_blank" rel="noopener"><Field acf_editor=partner_request_subject /></a>
      </div>
      <div class="col-md-5">
        <Field acf_editor=partner_request_details />
      </div>
      <div class="col-md-2">
        <Field acf_editor=request_solution /></div>
    </div> 

</If>

  </Loop>

</Loop>
1 Like