Skip Navigation

[Resolved] show user (author) profile with published posts

This support ticket is created 5 years, 7 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

Tagged: 

This topic contains 34 replies, has 4 voices.

Last updated by Nigel 5 years, 7 months ago.

Assisted by: Nigel.

Author
Posts
#1222983

I use the author meta in this content template: PRODUCATOR - author page infos in archive. It works if there are posts in the view loop. But when the loop is empty only appears No items found. Though I added the meta outside the loop:

[wpv-layout-start]
	[wpv-post-body view_template="producator-author-page-infos-in-archive"]
	[wpv-items-found]
	<!-- wpv-loop-start -->
	<ul class="wpv-loop js-wpv-loop">
		<wpv-loop>
			<li>[wpv-post-body view_template="loop-item-in-author-archives"]</li>
		</wpv-loop>
	</ul>
	<!-- wpv-loop-end -->
	[wpv-pager-archive-nav-links output="bootstrap" previous_next_links="true"]
	[/wpv-items-found]
	[wpv-no-items-found]
		<strong>[wpml-string context="wpv-views"]No items found[/wpml-string]</strong>
	[/wpv-no-items-found]
[wpv-layout-end]

I need the remove_unvetted_authors function to be sure that I show only the posts of users who are already of Producator role. When somebody registers it registers to Producator neautorizat (means unauthorized producer) role because the admins have to check for the provided data. And after that their posts appear in the views that are outside their own profile page. I don't know how could I modify it to not interfere with my views. At the moment the archive view seems alright but there also appears author != 15 in the query. And I just checked who user 15 is, and that's a registration I created for test and has 1 post published as Productor neautorizat. After this I changed the role to Producator.

#1223119

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Right. Outside of the Loop the author info comes from the first result in the Loop, and so if there are no results then there is no author info.

That is a shortcoming that is comparable to the problem on taxonomy archives when there are no matching posts the term of the archive is not available outside of the Loop. I'm going to comment on the same internal ticket to make it clear that the author archive has the same issue.

In the meantime it is necessary to use custom code to retrieve the author id, which you should be able to obtain from the $wp_query object.

Looking at your template I see you are using a custom shortcode [get_id_on_author_archive], so it looks like you are in the process of implementing the same yourself.

#1223139

That part with the ID is working. If there are posts I can get all the meta info. But what can I do when there are no posts? I don't understand that part. And this being an author info page, I have to show the author info even when there aren't any posts...

#1223742

I use this function to create an author id shortcode:

function func_get_id_in_author_archive($atts) {
  $author_id = 0;
  if (is_author()){
      $author_id = get_query_var('author');
    
  }
  return $author_id;
}
add_shortcode("get_id_on_author_archive", "func_get_id_in_author_archive");

And this is the content template that gets me the author meta info:

<h1 class="entry-title">[wpv-user field="user_firstname" id="[get_id_on_author_archive]"] [wpv-user field="user_lastname" id="[get_id_on_author_archive]"]</h1>
<p>
<i class="fas fa-home"></i> [types usermeta="localitate" user_id="[get_id_on_author_archive]"][/types]<br>
<i class="fas fa-phone-square"></i> <a href="tel:[types usermeta='telefon' user_id='[get_id_on_author_archive]'][/types]">[types usermeta="telefon" user_id="[get_id_on_author_archive]"][/types]</a><br>
<i class="fas fa-envelope-square"></i> <a href="mailto:[wpv-user field='user_email' id='[get_id_on_author_archive]']">[wpv-user field='user_email' id='[get_id_on_author_archive]']</a><br>
<i class="fas fa-globe"></i>  <a href="[types usermeta='site' output='raw' user_is_author='true' user_id='[get_id_on_author_archive]'][/types]">[types usermeta='site' output='raw' user_is_author='true' user_id='[get_id_on_author_archive]'][/types]</a>
</p>

As you see I always use the shortcode to give the ID. And this should work even is I don't have any posts in the archive. But it is not showing anything in that case. I added the content template to the no items found area too:

	[wpv-no-items-found]
		[wpv-post-body view_template="producator-author-page-infos-in-archive"]
		<strong>[wpml-string context="wpv-views"]No items found[/wpml-string]</strong>
	[/wpv-no-items-found]

But nothing... I shows just No items found...

#1223794

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Screenshot 2019-04-01 at 10.28.53.png

Hi Jozsef

Here is the code I used on my test site, registering a shortcode authorid to return the id of the author on an archive page.

add_shortcode('authorid', function () {

    global $wp_query;
    $authorid = $wp_query->get_queried_object_id();

    return $authorid;

});

I registered that shortcode for use in a shortcode attribute at Toolset > Settings > Front-end content, then used it outside of the Loop in my archive like so:

[wpv-layout-start]
<p>AUTHOR ID: [authorid]</p>
<p>[wpv-user field="display_name" id="[authorid]"]</p>
	[wpv-items-found]
	<!-- wpv-loop-start -->
		<wpv-loop>
          <h3>[wpv-post-link]</h3>
          <p>[wpv-post-author]</p>
		</wpv-loop>
	<!-- wpv-loop-end -->
	[/wpv-items-found]
	[wpv-no-items-found]
		<strong>[wpml-string context="wpv-views"]No items found[/wpml-string]</strong>
	[/wpv-no-items-found]
[wpv-layout-end]

You can see the results in the screenshot when there are no posts by that author.

#1223815

I almost wrote you back that it is not working but I experimented a little more and it is not working if I leave the author info in the content template and I insert that. When I put the code directly before the loop it works. That's fine for me, I just wanted to be more modular. But this could be a bug maybe that may interest the developers.

#1223829

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Sorry, could you clarify exactly what you mean by that last update?

Where are you inserting the author info content template?

#1223840

I kept the author meta-info in a content template: producator-author-page-infos-in-archive. Of course I changed the id shortcode. And this was not working.

[wpv-layout-start]
	[wpv-post-body view_template="producator-author-page-infos-in-archive"]
	[wpv-items-found]
	<!-- wpv-loop-start -->
	<ul class="wpv-loop js-wpv-loop">
		<wpv-loop>
			<li>[wpv-post-body view_template="loop-item-in-author-archives"]</li>
		</wpv-loop>
	</ul>
	<!-- wpv-loop-end -->
	[wpv-pager-archive-nav-links output="bootstrap" previous_next_links="true"]
	[/wpv-items-found]
	[wpv-no-items-found]
			[wpv-post-body view_template="producator-author-page-infos-in-archive"]
		<strong>[wpml-string context="wpv-views"]No items found[/wpml-string]</strong>
	[/wpv-no-items-found]
[wpv-layout-end]

But I tried to copy the code from the content template, and this works now. I just said that I guess this could be a bug.
But for me it's ok like this.

[wpv-layout-start]
	<h1 class="entry-title">[wpv-user field="user_firstname" id="[authorid]"] [wpv-user field="user_lastname" id="[authorid]"]</h1>
      <p>
      <i class="fas fa-home"></i> [types usermeta="localitate" user_id="[authorid]"][/types]<br>
      <i class="fas fa-phone-square"></i> <a href="tel:[types usermeta='telefon' user_id='[authorid]'][/types]">[types usermeta="telefon" user_id="[authorid]"][/types]</a><br>
      <i class="fas fa-envelope-square"></i> <a href="mailto:[wpv-user field='user_email' id='[authorid]']">[wpv-user field='user_email' id='[authorid]']</a><br>
      <i class="fas fa-globe"></i>  <a href="[types usermeta='site' output='raw' user_is_author='true' user_id='[authorid]'][/types]">[types usermeta='site' output='raw' user_is_author='true' user_id='[authorid]'][/types]</a>
      </p>
	[wpv-items-found]
	<!-- wpv-loop-start -->
#1223999

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

I moved my fields from the Loop Editor into a standalone content template and then inserted the content template, like so:

[wpv-layout-start]
[wpv-post-body view_template="author-profile"]
	[wpv-items-found]

It still worked the same as before, so I suspect in your case you have a problem with your shortcode.

Try just returning the author id to test it and confirm that it outputs the correct value in different contexts.

It needs to get the author id from the main query on the page.

#1226213

For me it was not was not working with content template and the shortcode was correct, but it is working with the author meta inline in the archive so it's ok for me like that. I don't have more time for this project.

Thank you Nigel and Christian for the help!

#1226214

My issue is resolved now. Thank you!

#1226246

Hi Nigel

I just closed the ticket but after that I found out I have exactly the same problem as described in the first part of our conversation.

I try to show in the anunturi post type's single template a view that lists 3 random posts from other posts of the same author. No option works from the query filter for filtering by author, I tried them all. This should work:

[wpv-view name="single-alte-produse-de-la-producator" author="[wpv-post-author format='meta' meta='ID']"]

I set the view to get the id by views shortcode attribute. I get the author meta infos in this template with the same approach so the ID should be correct. BUT I see posts from every author.

I realized that this is exactly the same problem I had when I tried to create an author archive. Because of a mod security setting in my hosting in could not create an author archive. So I created simply a view to list all the posts of a given author. And I had the same problem. I thought that because in archive the view works correctly that's alright by me but I forgot that in the single template I also have to filter the query by author...

Please help me Nigel to solve this because I just can't solve it by myself.

#1226269

I can confirm that when I remove this function from my functions.php the view works: https://toolset.com/forums/topic/show-user-author-profile-with-published-posts/#post-1222979. So you were totally right that this causes the problem in the query.

function remove_unvetted_authors( $query ) {
    if ( !is_admin() && $query->is_archive() && !is_page( 31 ) ) {
        $user_ids = get_users( [
            'role'   => 'producator_neautorizat',
            'fields' => 'ID'
        ] );
 
        $query->set( 'author__not_in', $user_ids );
    }
}
add_action( 'pre_get_posts', 'remove_unvetted_authors' );

Can you help me adjust this function that it won't interfere with the query but helps me to show in the anunturi archive only the posts created by PRODUCATOR role? I'd got this solution in a former ticket from Waqar: https://toolset.com/forums/topic/query-filter-for-user-role/.

#1226417

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

In that custom code this line determines where the code runs:

if ( !is_admin() && $query->is_archive() && !is_page( 31 ) )

!is_admin means only run the code on front-end pages

$query->is_archive() means run this code on archive pages (any kind of archive)

!is_page (31) means don't run on page id = 31, which is redundant because we already established we are on an archive page (of some kind) not a static page.

So you need to refine the conditions of where this code runs so that it only runs where you want it to and doesn't pollute other archive pages.

Let me know where you want that code to run and I'll help update the conditions.

#1226423

The idea is that on the front-end I don't want to show the post of producator neautorizat role. Except the account page, which has the id=31. This way the conditions should be good. Or am I understanding it wrong?