WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have my single-property.php showing custom post type 'property', within this page I want to also show the custom post type 'agent' that belongs to the author of property, each author only has one post of custom post type 'agent'.

I imagine it needs to go something like this, single-property.php with the query to show custom post type 'property', get author from first query and run second query showing all posts of custom post type 'author' by the same author.


share|improve this question
up vote 0 down vote accepted

You can use the $post->post_author,

so before your loop add $agent = ''; then inside the loop add

$agent = $post->post_author;

and then create a second query after you loop ends:

$agent_query = NEW WP_Query(array('post_type' => 'agent', 'author' => $agent));
while ($agent_query->have_posts()){
 //do your agent loop here
share|improve this answer
Won't reset do more harm than good here? You are not modifying globals, so reseting them after this will more likely break outer query, than cleanup after inner. – Rarst Jul 2 '11 at 14:05
What about $post as far as i know it's being modified , and also if the query id ran after the main loop then it will reset to the main query, could I be wrong? – Bainternet Jul 2 '11 at 14:14
For $post there is wp_reset_postdata() and I don't think it matter as much if queries are nested (if I understand question correctly) - it will be changed right away by next iteration of outer query. Anyway just a passing thought, it's up to Chuck to test this and make sure it works as he needs it to. – Rarst Jul 2 '11 at 14:28
Nested? i assumed its a single post view so i wrote my answer like i did (saying on loop after another) not in a nested way, and i guess you are right about reset_postdata() and that it's all up to Chuck. – Bainternet Jul 2 '11 at 14:33
Yeah, likely I misunderstood about nesting, but I stick by reseting only as much as was modified. Approach makes most sense to me. – Rarst Jul 2 '11 at 14:44

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.