• sam

    (@samjoelnang)


    The author’s archive (author.php) on the website I’m working on displays only the posts by the current author.

    I have a custom taxonomy called “co_writer” which I use to simply tag a post as having been co-written by another author. The “co_writer” terms basically share the same names and slugs with actual authors. For example, John Doe (author) has a “co_writer” term counterpart John Doe.

    I need a code that will allow me to also display on an author’s archive posts that have that author’s name used as the term for the “co_writer” taxonomy. So if John Doe has been tagged as co-writer in a post mainly credited to Jane Doe, that post will also show up on John Doe’s author archive. Right now, the posts only appear on the main author’s archive since I lack the code that will grab those into the intended co-author’s page as well.

    Is this possible? I was told pre_get_posts might be of use in this case, but I don’t know how exactly I can make it work. Thanks to anyone who can help.

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    “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 9 months, 3 weeks ago by sam.
    Moderator bcworkz

    (@bcworkz)

    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.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Include posts under a custom taxonomy in author.php’ is closed to new replies.