Skip to content

Instantly share code, notes, and snippets.

@JayWood
Last active February 10, 2023 22:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JayWood/866326e76796732b470c3b11ffc2c944 to your computer and use it in GitHub Desktop.
Save JayWood/866326e76796732b470c3b11ffc2c944 to your computer and use it in GitHub Desktop.
Query by co-authors for Co-Authors Plus
<?php
function query_by_co_authors() {
$current_user = wp_get_current_user();
// Your standard query
$post_args = array(
'post_type' => 'post',
'posts_per_page' => 10,
);
/**
* Co-Authors stores post authors in a hidden taxonomy
* so we need to query against that if possible, if not we
* just use the usual post_author field
*/
if ( class_exists( 'coauthors_plus' ) ) {
global $coauthors_plus;
$author_term = $coauthors_plus->get_author_term( $current_user );
$post_args['tax_query'] = array(
array(
'taxonomy' => $coauthors_plus->coauthor_taxonomy,
'field' => 'name',
'terms' => $author_term->name,
),
);
} else {
$post_args['author'] = $current_user->ID;
}
return $post_args;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment