“pre_get_posts” action is the right place to alter an author query. Be sure you’re only altering an author query since all post queries go through this action hook. Do so by checking which query vars are set.
You’ll then want to set the “tax_query” query var to an appropriate array of taxonomy term criteria. There are a number of examples in the linked section.
Are all of an author’s posts tagged with their name even if there is no co-author? If so, it’ll make modifying the query simpler. You can unset the author query var and rely solely upon the tags to get the right posts. If not, this gets trickier. You’ll want all posts that have this person as the author OR posts where they are tagged. But when you add a “tax_query” query var to an existing author query var, the two criteria use AND logic, not OR.
In order to use OR logic instead, you’ll need to also use the “posts_request” filter to alter the actual SQL used, finding the correct AND, then changing it to OR. There will be multiple ANDs in the query, be sure you only change the correct one.
Thread Starter
sam
(@samjoelnang)
Thank you for your insightful response, @bcworkz. Someone suggested I use the following code, but this didn’t work. To clarify, I don’t set any co_writer term when there is no co-author for a post. My objective is to make posts tagged with an author’s “namesake co_writer term” on that author’s archive page. Here is the existing code, which I think has the logic but can’t quite tell why it still retrieves only posts mainly written by the current author.
function wp_posts_on_author_archive($query) {
// Check if this is the main query and an author archive
if ($query->is_main_query() && $query->is_author()) {
// Get the current author's ID
$author_id = get_query_var('author');
// Get the current author's name
$author_name = get_the_author_meta('user_nicename', $author_id);
// Modify the tax query to include posts with the current author's name as a term in "co_writer"
$tax_query = array(
'relation' => 'OR',
array(
'taxonomy' => 'co_writer',
'field' => 'slug',
'terms' => $author_name,
),
array(
'taxonomy' => 'co_writer',
'field' => 'name',
'terms' => $author_name,
),
);
$query->set('tax_query', $tax_query);
}
}
// Hook into the pre_get_posts action
add_action('pre_get_posts', 'wp_posts_on_author_archive');
-
This reply was modified 2 years, 3 months ago by
sam.
I wrote the last two paragraphs, but then I noticed something questionable in your code. Are you sure you have user meta keyed “user_nicename”? There is none by default, user_nicename is a WP_User property saved in wp_users table. If I’m right, $author_name does not have a valid value and thus the tax_query part is ineffective.
Also, are you sure user_nicename is the right field in either table? It’s not often used. While WP_User->user_nicename is normally the same as the login name, this is not guaranteed. If you do have user_nicename as a meta value, or if you’ve made appropriate corrections and it still doesn’t work, read on…
I’m kind of surprised that code even retrieves the author’s posts. AFAIK the resulting query is something like (as overly simplified pseudo-SQL):
"SELECT posts WHERE post_author = $author_id AND (term_relationships.term_taxonomy_id = $slug_tt_id OR term_relationships.term_taxonomy_id = $name_tt_id)"
In any case, you’re on the right track, the next step is to determine why this is not behaving as expected. I think the best way to do so is to examine the actual SQL that this results from. You can get it from the global $wp_query->request property, or use the Query Monitor plugin. In the plugin’s info panel, go to the DB queries and use the main query pick from the caller filter.