Skip Navigation

[Resolved] Customize the Author Archive posts loop

This support ticket is created 2 years, 9 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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 6 replies, has 3 voices.

Last updated by umbertoZ 2 years, 9 months ago.

Assisted by: Waqar.

Author
Posts
#2279825

Hi, I would like to customize the author archive posts loop. On author pages I'm displaying all the posts of the selected author, but I would like to include also posts where this user is co-author.

I created a custom post field for posts, where I can select the co-author. It is a select with a list of user_login names.

How can I include in the Author Archive posts loop all the posts where the current user's user_login is the same as the co-author custom field value?

Would you suggest a different approach?

thanks
Umberto

#2279949

Nigel
Supporter

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

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

You can use pre_get_posts to modify the query on an archive page.

For an author archive, I expect that the query will be based on the post_author field. You would need to remove that condition, and add a meta_query argument to test the value of the custom field.

Does the custom field have entries for both authors?

If not this would be problematic, as if you retain the post_author condition and add a meta_query test, both conditions would have to be met for the returned posts.

https://developer.wordpress.org/reference/hooks/pre_get_posts/

#2280051

Hi, I built 2 views that outputs a list of IDs separated by commas of:

View 1 - all the author's posts
View 2 - all the posts where the author is co-author

Then I tried to set up the pre_get_posts function:

function post_types_author_archives($query) {
  
  if ($query->is_author) {
  
    if(get_query_var('author_name')) {
      $author_info = get_user_by('slug', get_query_var('author_name'));
    } else {
      $author_info = get_userdata(get_query_var('author'));
    }

    $user_id = $author_info->ID;
    $user_nicename = $author_info->user_nicename;

    $posts_ids = do_shortcode('[wpv-view name="posts-ids-by-author" author="'.$user_id.'"]');
    $posts_reviewed_ids = do_shortcode('[wpv-view name="posts-ids-by-reviewed-by" reviewedby="'.$user_nicename.'"]');
        
    $posts = $posts_ids.','.$posts_reviewed_ids

    $query->set( 'post__in', array( $posts ) );

  } else {

    remove_action( 'pre_get_posts', 'custom_post_author_archive' );
  }
}
add_action('pre_get_posts', 'post_types_author_archives', 1000);

It doesn't work...

I get correctly $user_id and $user_nicename, and I tested both views in a page, but when I run the Author archive I get a critical error and this message in the Debug.log:

PHP Fatal error: Allowed memory size of 134217728 bytes exhausted

I've also made a quick test with a simpler code:

function post_types_author_archives($query) {
  
  if ($query->is_author) {

   $query->set( 'post__in', array( '9891,6877' ) );

  } else {

    remove_action( 'pre_get_posts', 'custom_post_author_archive' );
  }
}
add_action('pre_get_posts', 'post_types_author_archives', 1000);

But it also doesn't work, it doesn't add posts with ID 9891,6877 to the author page.

Can you help me?

#2280607

Hi,

Thanks for writing back.

On my test website, I was able to make this simple function work to show only the selected posts (using the "post__in") on the author archive page, regardless of their author:


function post_types_author_archives( $query ) {
	if ( !is_admin() && $query->is_main_query() ) {
		if ( $query->is_author() ) {
			unset($query->query_vars['author_name']);
			$query->set( 'post__in', array( 1,13,15 ) );
		}
	}
}
add_action( 'pre_get_posts', 'post_types_author_archives' );

The line "unset($query->query_vars['author_name']);" is the key here that basically tells the query to ignore the post author criteria and bring all the posts which are being requested, through "post__in".

I hope this helps and please let me know if you need any further assistance around this.

regards,
Waqar

#2280761

Hi Waqar, thanks for your help. Your code works fine to load the posts set by the IDs, but it creates other problems.

It seems it loses the author in the /author/ archive. If I'm not logged in it doesn't display the name of the author in the title and the description in the Archive header . There is also a strange CSS problem because the <div class="wp-block-toolset-views-wpa-editor"> is displayed with 0% width.

If I'm logged in, I always see my name in all the author's pages, it seems it is getting the current logged user.

I also tested your code (unset($query->query_vars['author_name']);) within my function.

First I tested this:

function post_types_author_archives($query) {

  if ($query->is_author) {

    if(get_query_var('author_name')) {
      $author_info = get_user_by('slug', get_query_var('author_name'));
    } else {
      $author_info = get_userdata(get_query_var('author'));
    }

    $user_id = $author_info->ID;
    $user_nicename = $author_info->user_nicename;

    log_me('$user_id: '.$user_id);
    log_me('$user_nicename: '.$user_nicename);

    unset($query->query_vars['author_name']);
    $query->set( 'post__in', array( 9891,6877 ) );
    
  } else {

    remove_action( 'pre_get_posts', 'custom_post_author_archive' );
  }
}
add_action('pre_get_posts', 'post_types_author_archives', 1000);

Here the starnge behaviour is that it display 2 times the log_me() output in debug.log and the second time the variables values are empty:

01-Feb-2022 13:44:02 UTC] $user_id: 34433
[01-Feb-2022 13:44:02 UTC] $user_nicename: john-doe
[01-Feb-2022 13:44:03 UTC] PHP Notice: Trying to get property 'ID' of non-object in /wp-content/toolset-customizations/custom.php on line 37
[01-Feb-2022 13:44:03 UTC] PHP Notice: Trying to get property 'user_nicename' of non-object in /wp-content/toolset-customizations/custom.php on line 38
[01-Feb-2022 13:44:03 UTC] $user_id:
[01-Feb-2022 13:44:03 UTC] $user_nicename:

Is it possible that unset(); re-run the pre_get_posts function?

Then I tried again including the Views to get the IDs list to pass to $query->set():

function post_types_author_archives($query) {

  if ($query->is_author) {

    if(get_query_var('author_name')) {
      $author_info = get_user_by('slug', get_query_var('author_name'));
    } else {
      $author_info = get_userdata(get_query_var('author'));
    }

    $user_id = $author_info->ID;
    $user_nicename = $author_info->user_nicename;
    log_me('$user_id: '.$user_id);
    log_me('$user_nicename: '.$user_nicename);

    $posts_ids = do_shortcode('[wpv-view name="posts-ids-by-author" author="'.$user_id.'"]');
    $posts_reviewed_ids = do_shortcode('[wpv-view name="posts-ids-by-reviewed-by" reviewedby="'.$user_nicename.'"]');

    $posts = $posts_ids.','.$posts_reviewed_ids;

    log_me('$posts: '.$posts);
    log_me('$posts_reviewed_ids: '.$posts_reviewed_ids);

    unset($query->query_vars['author_name']);

    $query->set( 'post__in', array( $posts ) );

  } else {

    remove_action( 'pre_get_posts', 'custom_post_author_archive' );
  }
}
add_action('pre_get_posts', 'post_types_author_archives', 1000);

This starts an infinite loop.

Is there a different approach?

#2281695

Thanks for the update.

Unfortunately, I'm out of ideas for manipulating the author archive's query to include posts from other authors, without affecting the regular author archive functionalities.

For more ideas and to see whether something like this is possible at all, I'll recommend consulting specialist WordPress development forums:
https://wordpress.stackexchange.com/
https://wordpress.org/support/forums/

One possible workaround can be to show two separate lists/sections of posts on the author archive page. First, the list of posts from the current author, using the default author archive loop. And the other list shows posts where the current author is the co-author and uses a Toolset post view, filtered by the custom field.

#2283611

Thank you!