WP_Query Integration
ThemeRuby Multi Authors extends WordPress's WP_Query to support querying posts by multiple authors.
Overview
The plugin modifies WP_Query to make standard author parameters work with multiple authors:
// Standard WordPress query - now works with multi-authors!
new WP_Query( array(
'author' => 5, // Posts where user 5 is any author (primary or co-author)
) );
Enabling WP_Query Support
Ensure these settings are enabled (default: enabled):
Settings > ThemeRuby Multi Authors
☑ Enable WP_Query author parameter support
☑ Enable WP_Query author__in parameter support
☑ Enable WP_Query author__not_in parameter support
☑ Enable WP_Query author_name parameter support
Supported Parameters
author
Query posts by a single author ID.
Syntax
array(
'author' => int, // User ID
)
Example
// Get posts by user ID 5 (as primary or co-author)
$query = new WP_Query( array(
'author' => 5,
'posts_per_page' => 10,
) );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
the_title( '<h2>', '</h2>' );
}
wp_reset_postdata();
}
Real-world use case:
// Author archive page
$author_id = get_queried_object_id();
$query = new WP_Query( array(
'author' => $author_id,
'posts_per_page' => 20,
'paged' => get_query_var( 'paged' ),
) );
author__in
Query posts by multiple authors (OR condition).
Syntax
array(
'author__in' => array, // Array of user IDs
)
Example
// Get posts by users 1, 5, or 8
$query = new WP_Query( array(
'author__in' => array( 1, 5, 8 ),
'posts_per_page' => 10,
) );
Real-world use case - Team posts:
// Get all posts by editorial team
$team_members = array( 12, 15, 23, 45 );
$query = new WP_Query( array(
'author__in' => $team_members,
'posts_per_page' => 20,
'orderby' => 'date',
'order' => 'DESC',
) );
author__not_in
Exclude posts by specific authors.
Syntax
array(
'author__not_in' => array, // Array of user IDs to exclude
)
Example
// Get all posts EXCEPT those by users 3 and 6
$query = new WP_Query( array(
'author__not_in' => array( 3, 6 ),
'posts_per_page' => 10,
) );
Real-world use case:
// Exclude guest authors from main loop
$guest_author_ids = array( 100, 101, 102 );
$query = new WP_Query( array(
'post_type' => 'post',
'author__not_in' => $guest_author_ids,
'posts_per_page' => 10,
) );
author_name
Query posts by author username (nicename).
Syntax
array(
'author_name' => string, // User nicename
)
Example
// Get posts by user with nicename 'john-doe'
$query = new WP_Query( array(
'author_name' => 'john-doe',
'posts_per_page' => 10,
) );
Real-world use case:
// Custom author page by username
$username = get_query_var( 'author_name' );
if ( $username ) {
$query = new WP_Query( array(
'author_name' => $username,
'posts_per_page' => 20,
) );
}
Combined Queries
Author + Category
// Posts by user 5 in 'news' category
$query = new WP_Query( array(
'author' => 5,
'category_name' => 'news',
'posts_per_page' => 10,
) );
Multiple Authors + Post Type
// Get WooCommerce products by specific authors
$query = new WP_Query( array(
'post_type' => 'product',
'author__in' => array( 1, 5, 8 ),
'posts_per_page' => 20,
) );
Author + Date Range
// Posts by user 5 from last 30 days
$query = new WP_Query( array(
'author' => 5,
'date_query' => array(
array(
'after' => '30 days ago',
),
),
'posts_per_page' => 10,
) );
Author + Meta Query
// Featured posts by specific authors
$query = new WP_Query( array(
'author__in' => array( 1, 5, 8 ),
'meta_query' => array(
array(
'key' => 'featured',
'value' => 'yes',
),
),
'posts_per_page' => 5,
) );
Author + Tag
// Posts by user 5 tagged 'tutorial'
$query = new WP_Query( array(
'author' => 5,
'tag' => 'tutorial',
'posts_per_page' => 10,
) );
Co-Authored Posts
/**
* Get posts co-authored by two specific users
*/
function get_coauthored_posts( $author1_id, $author2_id ) {
// Get posts by author 1
$posts1 = new WP_Query( array(
'author' => $author1_id,
'posts_per_page' => -1,
'fields' => 'ids',
) );
// Get posts by author 2
$posts2 = new WP_Query( array(
'author' => $author2_id,
'posts_per_page' => -1,
'fields' => 'ids',
) );
// Find intersection (posts by both)
$shared_post_ids = array_intersect(
$posts1->posts,
$posts2->posts
);
// Query the shared posts
if ( ! empty( $shared_post_ids ) ) {
return new WP_Query( array(
'post__in' => $shared_post_ids,
'posts_per_page' => -1,
) );
}
return new WP_Query( array( 'post__in' => array( 0 ) ) ); // Empty query
}
// Usage
$coauthored = get_coauthored_posts( 5, 8 );
Author Posts Widget
/**
* Display recent posts by current author
*/
function my_author_posts_widget() {
if ( ! is_single() ) {
return;
}
$authors = tmauthors_get_post_authors( get_the_ID() );
if ( empty( $authors ) ) {
return;
}
$first_author = $authors[0];
$query = new WP_Query( array(
'author' => $first_author->ID,
'posts_per_page' => 5,
'post__not_in' => array( get_the_ID() ), // Exclude current post
) );
if ( $query->have_posts() ) {
echo '<div class="author-recent-posts">';
echo '<h3>More from ' . esc_html( $first_author->display_name ) . '</h3>';
echo '<ul>';
while ( $query->have_posts() ) {
$query->the_post();
echo '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>';
}
echo '</ul>';
echo '</div>';
wp_reset_postdata();
}
}
Team Archive Page
/**
* Custom template for team archive page
* Template Name: Team Posts Archive
*/
function team_archive_template() {
// Get team member IDs (from custom meta or hardcoded)
$team_ids = get_option( 'team_member_ids', array() );
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$query = new WP_Query( array(
'author__in' => $team_ids,
'posts_per_page' => 20,
'paged' => $paged,
'orderby' => 'date',
'order' => 'DESC',
) );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
get_template_part( 'template-parts/content', get_post_type() );
}
// Pagination
the_posts_pagination( array(
'total' => $query->max_num_pages,
) );
wp_reset_postdata();
}
}
Performance Optimization
Caching Author Queries
function get_cached_author_posts( $author_id, $limit = 10 ) {
$cache_key = 'author_posts_' . $author_id . '_' . $limit;
$posts = wp_cache_get( $cache_key, 'author_queries' );
if ( false === $posts ) {
$query = new WP_Query( array(
'author' => $author_id,
'posts_per_page' => $limit,
) );
$posts = $query->posts;
// Cache for 1 hour
wp_cache_set( $cache_key, $posts, 'author_queries', 3600 );
}
return $posts;
}
Using Fields Parameter
// Get only post IDs for better performance
$query = new WP_Query( array(
'author__in' => array( 1, 5, 8 ),
'fields' => 'ids', // Only return IDs
) );
// Get only specific fields
$query = new WP_Query( array(
'author' => 5,
'fields' => array( 'ID', 'post_title', 'post_date' ),
'posts_per_page' => 100,
) );
Modifying Main Query
In functions.php
/**
* Modify main author archive query
*/
add_action( 'pre_get_posts', 'modify_author_archive' );
function modify_author_archive( $query ) {
// Only modify main query on author archives
if ( ! is_admin() && $query->is_main_query() && $query->is_author() ) {
// Show more posts
$query->set( 'posts_per_page', 30 );
// Exclude specific categories
$query->set( 'category__not_in', array( 5, 10 ) );
// Order by title instead of date
$query->set( 'orderby', 'title' );
$query->set( 'order', 'ASC' );
}
}
REST API Integration
Custom Endpoint
/**
* Add custom REST endpoint for author posts
*/
add_action( 'rest_api_init', function() {
register_rest_route( 'myapp/v1', '/author/(?P<id>\d+)/posts', array(
'methods' => 'GET',
'callback' => 'get_author_posts_rest',
) );
} );
function get_author_posts_rest( $request ) {
$author_id = $request['id'];
$query = new WP_Query( array(
'author' => $author_id,
'posts_per_page' => 10,
) );
$posts = array();
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
$posts[] = array(
'id' => get_the_ID(),
'title' => get_the_title(),
'excerpt' => get_the_excerpt(),
'link' => get_permalink(),
);
}
wp_reset_postdata();
}
return rest_ensure_response( $posts );
}
Troubleshooting
Query Not Working?
Check settings:
Settings > ThemeRuby Multi Authors
Ensure WP_Query support is enabled
Clear cache:
// Clear object cache
wp_cache_flush();
// Clear transients
delete_transient( 'my_transient_key' );
Test with simple query:
// Debug query
$query = new WP_Query( array(
'author' => 5,
) );
// Check SQL
echo $query->request;
Performance Issues?
Use pagination:
// Bad - loads all posts
$query = new WP_Query( array(
'author' => 5,
'posts_per_page' => -1, // Avoid this!
) );
// Good - uses pagination
$query = new WP_Query( array(
'author' => 5,
'posts_per_page' => 20,
'paged' => $paged,
) );
Use fields parameter:
// Only get IDs for faster queries
$query = new WP_Query( array(
'author' => 5,
'fields' => 'ids',
) );
Complete Example: Author Filter Widget
/**
* Widget to filter posts by multiple authors
*/
class Multi_Author_Filter_Widget extends WP_Widget {
public function widget( $args, $instance ) {
$selected_authors = isset( $_GET['filter_authors'] ) ? $_GET['filter_authors'] : array();
// Display author checkboxes
$authors = tmauthors_get_assignable_authors();
echo '<form method="get">';
echo '<h4>Filter by Authors</h4>';
foreach ( $authors as $author ) {
$checked = in_array( $author->ID, $selected_authors ) ? 'checked' : '';
echo '<label>';
echo '<input type="checkbox" name="filter_authors[]" value="' . $author->ID . '" ' . $checked . '>';
echo esc_html( $author->display_name );
echo '</label><br>';
}
echo '<button type="submit">Filter</button>';
echo '</form>';
// Display filtered posts
if ( ! empty( $selected_authors ) ) {
$query = new WP_Query( array(
'author__in' => $selected_authors,
'posts_per_page' => 10,
) );
if ( $query->have_posts() ) {
echo '<h4>Results:</h4>';
while ( $query->have_posts() ) {
$query->the_post();
echo '<div>' . get_the_title() . '</div>';
}
wp_reset_postdata();
}
}
}
}
Next Steps
- Template Tags - Display author information
- Hooks & Filters - Customize query behavior
- Theme Integration - Complete examples