CMS Development

Tajwar
Participante

Blog authors function only returning 3 author

resolver

I am trying to fetch all the blog authors with blog_authors("default", 250) function, but this is only returning 3 authors. I am using that in a module. Here is my full code.

{{ blog_authors("default", 20) }}

{% set my_authors = blog_authors("default", 250) %}
<ul>
{% for author in my_authors %}
<li><a href="{{ blog_author_url(group.id, author.slug) }}">{{ author }}</a></li>
{% endfor %}

I also tried this with Hubspot CMS Api which is returning the correct result. I am attaching a screenshot of the result here.

Tajwar_0-1660752842080.png

Also apology, had to hide some contents because of privacy issues.

0 Me gusta
1 Soluciones aceptada
BarryGrennan
Solución
Colaborador líder

Blog authors function only returning 3 author

resolver

So, the issue is that John Smith author on blog A and John Smith author on blog B are unique.

 

If you look at the JSON it spits out, it's the livePosts value that causes the issue.

Here's a simplified version of adding the Author names to an array if it's unique:

 

 

{% set my_authors1 = blog_authors(34471392197, 250) %}
{% set my_authors2 = blog_authors(59761752650, 250) %}
{% set all_authors = (my_authors1 + my_authors2)|sort(False, False, "display_name") %}
{% set unique_authors = []%}

{% for author in all_authors %}
{% unless author.display_name in unique_authors %} 
      {% do unique_authors.append( author.display_name ) %}
{% endunless %}
{% endfor %}

 

 

 

Now that's simply just an array of their names. If you want the other author info it's going to get more complicated.

I tried playing around with 

 

 

{% unless author.display_name in unique_authors|??????? %} 
      {% do unique_authors.append( author ) %}
{% endunless %}

 

 

trying to figure out how to search inside the nested arrays of the unique_authors array using attr and the like but I'm not coming up with anything.


profile2022aBarry Grennan

Freelance HubSpot CMS Developer

Website | Contact

 

 

Ver la solución en mensaje original publicado

5 Respuestas 5
BarryGrennan
Colaborador líder

Blog authors function only returning 3 author

resolver

Are all of those authors on your "default" blog?

 

My guess would be the api is getting all authors for the entire portal

 

{% set my_authors = blog_authors("default", 250) %} only pulls in authors from the default blog.

 


profile2022aBarry Grennan

Freelance HubSpot CMS Developer

Website | Contact

 

 

Tajwar
Participante

Blog authors function only returning 3 author

resolver

Thanks for helping me. Can you tell me how can I get authors from all blogs together?

0 Me gusta
BarryGrennan
Colaborador líder

Blog authors function only returning 3 author

resolver

Hi @Tajwar,

 

I don't believe there is a native way to do it.

 

What you'd do is set a blog_authors call for each blog id then set a variable that combined them like "set combined = (authors1 + authors2)"

 

Of course you might also need to create an array from that to weed out duplicates that may be authors on both blogs.

 

I'm on my mobile right now, so writing the code would take me forever 😂 but I'll be back in office this afternoon and can give you a sample code then


profile2022aBarry Grennan

Freelance HubSpot CMS Developer

Website | Contact

 

 

 

 

 

 

Tajwar
Participante

Blog authors function only returning 3 author

resolver

I was trying to do the same thing. However, it seems unique / union doesn't work. Maybe there is some kind of underlying object which is preventing it from detecting same author from different blog.

0 Me gusta
BarryGrennan
Solución
Colaborador líder

Blog authors function only returning 3 author

resolver

So, the issue is that John Smith author on blog A and John Smith author on blog B are unique.

 

If you look at the JSON it spits out, it's the livePosts value that causes the issue.

Here's a simplified version of adding the Author names to an array if it's unique:

 

 

{% set my_authors1 = blog_authors(34471392197, 250) %}
{% set my_authors2 = blog_authors(59761752650, 250) %}
{% set all_authors = (my_authors1 + my_authors2)|sort(False, False, "display_name") %}
{% set unique_authors = []%}

{% for author in all_authors %}
{% unless author.display_name in unique_authors %} 
      {% do unique_authors.append( author.display_name ) %}
{% endunless %}
{% endfor %}

 

 

 

Now that's simply just an array of their names. If you want the other author info it's going to get more complicated.

I tried playing around with 

 

 

{% unless author.display_name in unique_authors|??????? %} 
      {% do unique_authors.append( author ) %}
{% endunless %}

 

 

trying to figure out how to search inside the nested arrays of the unique_authors array using attr and the like but I'm not coming up with anything.


profile2022aBarry Grennan

Freelance HubSpot CMS Developer

Website | Contact