You’re correct in that doing a normal WP_Query will only produce the posts where the author is set as the primary author. When your co-authors data is saved, the first author on the list is saved as the primary author, and all authors are saved as terms for the post under the ‘author’ taxonomy.
So, the simplest thing to do is to search for all posts with the author set as a taxonomy term. This works much like a normal ‘tag’ or ‘category’ query would.
Let me know if you have any questions as to what the code might look like.
I’ve found this post via google and identified my problem with the plugin as the same. I was not successful in writing the correct code for WP_Query, so I would really be glad to get an example here, as you offered.
Many thanks in advance!
@dpfuhl could you describe a bit more about what you’re trying to accomplish?
You offered to give a code example for WP_Query for the authors taxonomy term in your post above:
So, the simplest thing to do is to search for all posts with the author set as a taxonomy term. This works much like a normal ‘tag’ or ‘category’ query would.
Let me know if you have any questions as to what the code might look like.
This is what I would like to get, thanks!
Right now I’m using this query to get all posts of category ID 4 and the current author:
<?php
$author_ID = $post->post_author;
$args = array(
'cat' => '4',
'author' => $author_ID,
'post_type' => 'post',
'orderby' => 'meta_value',
'meta_key' => 'morefields_date',
'order' => 'ASC'
);
$events_query = new WP_Query( $args );
?>
The result is showing only posts were the current author is the primary one. But I would like to get all posts of the current author, of course.
Sure, you can use something like this:
<?php
$user_login = 'your-user-login';
$args = array(
'cat' => '4',
'post_type' => 'post',
'orderby' => 'meta_value',
'meta_key' => 'morefields_date',
'order' => 'ASC'
'tax_query' => array(
array(
'taxonomy' => 'author',
'field' => 'slug',
'terms' => $user_login,
),
),
);
$events_query = new WP_Query( $args );
?>
Success! Thank you very much!
Only some fixes have been neccessary:
<?php
$author_ID = $post->post_author;
$user_info = get_userdata($author_ID);
$user_login = $user_info->user_login;
$args = array(
'tax_query' => array(
array(
'taxonomy' => 'author',
'field' => 'slug',
'terms' => $user_login
)
),
'cat' => '4',
'post_type' => 'post',
'orderby' => 'meta_value',
'meta_key' => 'morefields_date',
'order' => 'ASC'
);
?>
For some reason the tax_query array has to be the first element in the args array. If not, there will be a parsing error.
Odd. Glad you figured it out!