0
votes

I have two custom post types…

BOOK and BOOK AUTHOR.

I want to…

LIST all BOOK's by an author on their respective AUTHOR PAGE.

So if i have 10 books by Stephen KING. I want to list them all (and only those by him) on the STEPHEN KING page. I am having real trouble working out how to query posts to do this.

Any advice? I am using advanced custom fields plugin if that helps, but can't work out how to query and display this post information.

I currently use the following code to display ALL of my releases, but how do i get the specific ones on their specific author pages?

<?php
     $args=array(
     'post_type' => 'book',
     'post_status' => 'publish',
     'posts_per_page' => 12,
     'caller_get_posts'=> 1,
     'orderby'=> 'date',
     'order' => 'DESC'
     );
     $my_query = null;
     $my_query = new WP_Query($args);
     if( $my_query->have_posts() ) {
     echo '';
     $i = 0;
     while ($my_query->have_posts()) : $my_query->the_post();
     if($i % 6 == 0) { ?>
<div class="row">    
<?php
}
?>

<img src="<?php the_field('cover_image'); ?>" /><br />
<?php the_field('author_name'); ?><br />
<?php the_title(); ?><br />

</div>

<?php    
if($i % 6 == 0) { ?> 

<?php
}

$i++;
endwhile;
 } 
wp_reset_query();
?>
2

2 Answers

0
votes

I did something similar, but...

main problem here is, how are you categorizing those books? Did you create taxonomies for that? And those taxonomies are in fact author´s names? I mean, how you sincronize to tell a book belong to certain author?

0
votes

That is the trick. And then you can just query by category. In this case category must be same slug than author page slug. You will use that data to query post.

What i did in my case is create a function that once an author page is created, a taxonomt with same name and slug is created. So you dont need to manualy create those two things with same name and slug. Something like:

<?php$myposts = get_posts(array(    'showposts' => -1,     'post_type' => 'books',     'tax_query' => array(        array(        'taxonomy' => 'author_book',         'field' => 'slug',         'terms' => array(AUTHOR_SLUG));    )));

notice AUTHOR_SLUG could be an array, like "pete","paul".

"author_book" is just the taxonomy slug. In your case must be just "category_cat"

You can use those args with yout loop.