0

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

1 Answer 1

0

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();
10
  • 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
    Commented Jul 2, 2011 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
    Commented Jul 2, 2011 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
    Commented Jul 2, 2011 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
    Commented Jul 2, 2011 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
    Commented Jul 2, 2011 at 14:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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