Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

Thanks

share|improve this question

1 Answer 1

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()){
 $agent_query->the_post();
 //do your agent loop here
}
wp_reset_postdata();
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

 
discard

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.