0

This is what I have:

  • Custom Post type "Books" with Taxononies/Categories "Genre" and "Author"
  • Another Custom Post Type "Authors" which have all author pictures and bios.

This is what I want to do:

  • On the single "Book" page, have a sidebar with the books "Author" picture and bio.

I know how to get the "Author" Taxonomy of a "Book" post, but the information I need is from the "Author" Custom Post Type with the same name. The only thing i can think of is that the slug for both would be the same (i.e. if the books author was "John Smith", the taxonomy would be "john-smith" and the post that I need to query from my "Authors" Custom Post Type would also be "john-smith"). I just don't know how to go about getting it or if there is another way of doing it.

I think it should be something like this:

   $bookauthor = wp_get_post_terms($post->ID, 'author', array("fields" => "slug"));
   $custom_query = new WP_Query(array(
   'post_type' => 'authors',
   'name' => $bookauthor

or this:

   $bookauthor = wp_get_post_terms($post->ID, 'author');
   $bookauthorslug = $bookauthor[0]->slug;
   $custom_query = new WP_Query(array(
   'post_type' => 'author',
   'name' => $bookauthorslug

But neither works.

1 Answer 1

0

Probably you are doing it wrong.

When you need do have a one to many relation (one author has many books) taxonomy is not best way.

Also, according to WordPress structure, naming a taxonomy and and a post in same way is not a very reliable way.

Just for example thinks if two authors are homonyms: having two authors both named "John Smith" is perfectly normal, in that case sure WordPress will assign unique slug to both taxonomy term and post slug, but for you will be hard assign right taxonomy terms on post creation.

Best way is to create a custom field for the book post type, that you can name 'author_id' and where during book post creation you insert the righr post id, after that, in the book page you can use:

$author_id = get_post_meta( $post->ID, 'author_id', true );
$author_post = get_post($author_id);

Sure this is not very elegant solution, but creating a custom metabox to set the meta field that show a dropdown of author post, is easy and will be elegant and pretty to see.

If you need help on creating the metabox, look at questions tagged metabox in this site.

As alternative you can also consider the well known plugin Post 2 Posts that will do the work for you.


To just answer your question, your second code block is almost right, working version (version that should work) is:

global $post;
$bookauthor = wp_get_post_terms( $post->ID, 'author' );
if ( ! empty($bookauthor) ) {
  $slug = array_shift( $bookauthor );
  $custom_query = new WP_Query( array( 'post_type' => 'author','name' => $slug->slug ) );
  // here your loop
  while ( $custom_query->have_posts() ) { $custom_query->the_post();
     // here post output
  }
  wp_reset_postdata();
}

This because IIRC wp_get_post_terms return an array using term ids as array keys.

But, as already said, I don't think this is the right way to do it.

6
  • Thank you for your feedback. I realize it isn't ideal but there is other reasons why I'm doing it this way. When I try to use your ammended code, I get the following errror: "Warning: trim() expects parameter 1 to be string, object given in ../wp-includes/query.php on line 1452", followed by all posts with the "Author" custom post types (not just the one author I need). This was the same error I was getting with my code. Commented Oct 17, 2013 at 12:00
  • Thanks, but now I don't get anything returned Commented Oct 17, 2013 at 13:19
  • Got it. I changed $slug->term_id to $slug->slug and works perfectly. Thanks for your help! Commented Oct 17, 2013 at 13:24
  • Unfortunately its not working for new posts, lol. It returns all Authors. Commented Oct 17, 2013 at 13:58
  • Sorry, it is works now. However, if no Author taxonomy is selected on the Book post, it will display all Authors and if there is more than one Author selected (although rare) it will only display one Author. Commented Oct 17, 2013 at 15:47

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.