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.

SOLUTION PASTEBIN LINKS::

Please refer to these files here - solved by storing the post ID in a variable and reworking how I was nesting my loops.


I would like to display a single post (from a custom post type "authors") - then immediately underneath this post's content I want to display a list of other posts that I have assigned to this specific post (from a custom post type "books").

For the "books" custom post type I am currently using a custom meta box containing a dropdown (select field) - this field pulls in a list of published posts("authors") and saves the "author" post's ID to the meta field.

What I want to do on the single template is only show posts whose meta field value is equal to the ID of the post currently being display. Basically only show the list of "books" for this single "author" ... is this doable?


I would need to do the reverse as well ... so when viewing an archive of "book" posts I would like to show the "author" post associated with it underneath... so one loop to pull in the "book" content and nested inside is another loop to pull in the "author" post title, etc... I am assuming by comparing the content of a meta field against the post ID?

How do I store the ID of the "author" post to compare across loops? How do I store the meta field value to compare against the ID - again across loops or nested loops? Or If I am totally going the wrong way with this please enlighten me ;)

If anyone can point me in the right direction it would be most greatly appreciated!

share|improve this question

1 Answer 1

up vote 0 down vote accepted

For the author single page

$query = new WP_Query(array(
    'post_type' => 'books',
    'meta_key' => 'author',
    'meta_value' => get_the_ID() // assuming you're inside the loop
));

while($query->have_posts()) {
    $query->the_post();
    the_title();
}
wp_reset_query();

For the books archive page

// assuming you're inside the loop
$author = get_post(get_post_meta(get_the_ID(), 'author', true));
share|improve this answer
    
This ALMOST works for the Author Single... but is only shows one book (the newest published) and I need it to show all of them. I tried adding a 'posts_per_page' but it has no effect. –  henryleo Oct 20 '12 at 22:59
    
For the Book Archive it doesnt appear to be working - I think its my nested loops. One main basic loop with a custom query underneath to pull in the author. Its pulling the books just fine, but not the authors. Im gonna keep scrathing away- thank you! –  henryleo Oct 20 '12 at 23:01
    
CORRECTION - Your solution for Author Single DOES WORK!!! I simply had to rework how I was writing the custom query and now it functions correctly ... Thank You! ;) –  henryleo Oct 20 '12 at 23:29
    
for the book archive, try this get_post(intval(get_post_meta(get_the_ID(), 'author', true))); –  Mridul Aggarwal Oct 20 '12 at 23:35
    
Also I got your Book Archive solution working as well! I had to switch my main loop to a custom query... I will post the final code to PasteBin and add it to the original question. Thank you again! –  henryleo Oct 20 '12 at 23:40

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.