Skip to content

Commit

Permalink
- Add support for multiple authors posts when using get_posts() funct…
Browse files Browse the repository at this point in the history
…ion via 'ppma_filter' => true parameter #1591
  • Loading branch information
ojopaul committed Dec 20, 2023
1 parent e95a468 commit 1bf5d3b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/core/Classes/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,39 @@ public static function fix_query_pre_get_posts($wp_query)
$wp_query->set('is_guest', $is_guest);
}

/**
* Filter query for when ppma_filter is set to true
*
* @param WP_Query $query Query object.
*/
public static function filter_query_pre_get_posts($query)
{
if (isset($query->query['ppma_filter']) && $query->query['ppma_filter'] === true && !empty($query->query_vars['author'])) {
$author_id = $query->query_vars['author'];
$author = Author::get_by_user_id($author_id);

if ($author
&& is_object($author)
&& isset($author->term_id)
&& (int)$author->term_id > 0
) {
// Remove author from the query
$query->set('author', '');
// Add a taxonomy query
$query->set('tax_query', array(
array(
'taxonomy' => 'author',
'field' => 'term_id',
'terms' => $author->term_id,
),
));
}

}

return $query;
}

/**
* Fix for publishpress author pages post type.
*
Expand Down
6 changes: 6 additions & 0 deletions src/core/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,12 @@ public function __construct()
2
);

// Query filter for multiple authors
add_action(
'pre_get_posts',
['MultipleAuthors\\Classes\\Query', 'filter_query_pre_get_posts']
);

// Author search
add_action(
'wp_ajax_authors_search',
Expand Down

0 comments on commit 1bf5d3b

Please sign in to comment.