CMS Development

Tajwar
Teilnehmer/-in

Blog authors function only returning 3 author

lösung

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 Upvotes
1 Akzeptierte Lösung
BarryGrennan
Lösung
Stratege/Strategin

Blog authors function only returning 3 author

lösung

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

 

 

Lösung in ursprünglichem Beitrag anzeigen

5 Antworten
BarryGrennan
Stratege/Strategin

Blog authors function only returning 3 author

lösung

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
Teilnehmer/-in

Blog authors function only returning 3 author

lösung

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

0 Upvotes
BarryGrennan
Stratege/Strategin

Blog authors function only returning 3 author

lösung

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
Teilnehmer/-in

Blog authors function only returning 3 author

lösung

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 Upvotes
BarryGrennan
Lösung
Stratege/Strategin

Blog authors function only returning 3 author

lösung

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