I am currently trying to perform a custom query on the authors page (author.php). I have two custom fields for posts that I want to query against (post_photographer and post_videographer).
What I am trying to do for the author’s profile is get all the posts for the current user profile where the user is:
- the author of the post
- OR the post photographer
- OR the post videographer
So each person’s profile MAY have posts that they didn’t author (they were the photographer or videographer instead).
The following code is close to what I want, but the problem is that it retrieves posts where the current user is the author AND is either a post photographer OR post videographer. It needs to be where the current user is the author OR post photographer OR post videographer.
$args = array(
'author' => $posts[0]->post_author,
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'post_photographer',
'value' => $posts[0]->post_author,
'type' => 'numeric'
),
array(
'key' => 'post_videographer',
'value' => $posts[0]->post_author,
'type' => 'numeric'
)
)
);
query_posts( $args );
Is this possible via WordPress queries (query_posts or WP_Query), or do I need to write custom SQL? Any help is greatly appreciated! If you need clarification please ask.
For anyone who may have a similar need, I solved this the following way (on authors.php):
First I get the author ID:
I then created a custom query:
And finally use the following to get the results:
Here is a simplified version of my loop to display the results:
I hope this helps someone!
Either of these 2 filters should help you:
Check the examples in codex.