pre get posts - Modify author archive query to combine two queries

admin2025-06-06  4

I'm working on a site that allows multiple authors on posts by using an "additional authors" Posts to Posts connection type. Posts still have a standard author, but they can optionally have additional authors.

I'd like to modify the author archive query to include all posts by the author, including those where they are an "additional" author. This isn't as simple as just adding parameters to the main author query, so in pre_get_posts I'm combining two queries by getting the matching post IDs from both, then running a post__in query.

Thats works, but I can't figure out how to pass the results of that query to the author template. Changing the author query var means that the site doesn't show the author archive template.

tl;dr - can you use pre_get_posts to fully customize the author archive query, and include posts that are not actually authored by that user?

I'm working on a site that allows multiple authors on posts by using an "additional authors" Posts to Posts connection type. Posts still have a standard author, but they can optionally have additional authors.

I'd like to modify the author archive query to include all posts by the author, including those where they are an "additional" author. This isn't as simple as just adding parameters to the main author query, so in pre_get_posts I'm combining two queries by getting the matching post IDs from both, then running a post__in query.

Thats works, but I can't figure out how to pass the results of that query to the author template. Changing the author query var means that the site doesn't show the author archive template.

tl;dr - can you use pre_get_posts to fully customize the author archive query, and include posts that are not actually authored by that user?

Share Improve this question edited Nov 6, 2018 at 15:07 Chris Herbert asked Nov 6, 2018 at 1:00 Chris HerbertChris Herbert 3102 silver badges7 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

The only way that I've found to override the main author archive query in a way that will include posts that aren't created by the specified user is by using the posts_where filter.

For example:

        $post_ids = function_that_gets_desired_post_ids();

        add_filter('posts_where', function($where) use ($post_ids){

            $post_ids = array_map(function($id){
                return (int) $id;
            }, $post_ids);

            $in = implode(',', $post_ids);
            $where .= " OR (ID IN ($in) AND post_type = 'post')";
            return $where;

        });

Using something like this doesn't make me feel good, but it does work.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749202236a317220.html

最新回复(0)