$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>pre get posts - Modify author archive query to combine two queries|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin10PV0评论

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.

Post a comment

comment list (0)

  1. No comments so far