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?