I am wondering how to write code to order a list of posts by their terms from custom taxonomies?
Here is what I have so far:
- I have a page that lists all the posts in a Custom Post Type, on one page.
- Under each CPT Post, I have retrieved the Custom Taxonomies to show.
- I have allowed for the CPT posts to be sorted by Alpha, ASC and DESC.
In the end, it looks like this:
The Goal:
I would like to add a couple links with the "Sort By Title" ones I already have.
Specifically, I would like to order by (alpha) Director, Studio, and Episodes, because I know these are either strings or numeric.
I would also like to order by the Season Premiered, by year, but I may need to change the way I named those terms (It'll probably just spit out Winter 2011, Winter 2012 with the way it currently is).
Sorting by Genre is another matter entirely that I am not going to tackle because of the multiple terms.
I am not too sure how to go about this and could use the help.
The Page's Current Code:
<div class="content-container">
<a href="?sort=titleup">Sort By Title A-Z</a>
<a href="?sort=titledown">Sort By Title Z-A</a>
<hr>
<?php
$type = 'animes';
$args=array(
'post_type' => $type,
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1
);
if( isset( $_GET['sort'] ) && "titleup" == $_GET['sort'] ){
$args['orderby'] = 'title';
$args['order'] = 'ASC';
}
if( isset( $_GET['sort'] ) && "titledown" == $_GET['sort'] ){
$args['orderby'] = 'title';
$args['order'] = 'DESC';
}
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<div class="anime-title"><a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?> Page"><?php the_title(); ?></a></div>
<br><span>Director:</span>
<?php
$taxonomy = 'director';
// get the term IDs assigned to post.
$post_terms = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
// separator between links
$separator = ', ';
if ( !empty( $post_terms ) && !is_wp_error( $post_terms ) ) {
$term_ids = implode( ',' , $post_terms );
$terms = wp_list_categories( 'title_li=&style=none&echo=0&taxonomy=' . $taxonomy . '&include=' . $term_ids );
$terms = rtrim( trim( str_replace( '<br />', $separator, $terms ) ), $separator );
// display post categories
echo $terms;
}
?>
<br><span>Studio:</span>
<?php
$taxonomy = 'studio';
// get the term IDs assigned to post.
$post_terms = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
// separator between links
$separator = ', ';
if ( !empty( $post_terms ) && !is_wp_error( $post_terms ) ) {
$term_ids = implode( ',' , $post_terms );
$terms = wp_list_categories( 'title_li=&style=none&echo=0&taxonomy=' . $taxonomy . '&include=' . $term_ids );
$terms = rtrim( trim( str_replace( '<br />', $separator, $terms ) ), $separator );
// display post categories
echo $terms;
}
?>
<br><span>Season Premiered:</span>
<?php
$taxonomy = 'season';
// get the term IDs assigned to post.
$post_terms = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
// separator between links
$separator = ', ';
if ( !empty( $post_terms ) && !is_wp_error( $post_terms ) ) {
$term_ids = implode( ',' , $post_terms );
$terms = wp_list_categories( 'title_li=&style=none&echo=0&taxonomy=' . $taxonomy . '&include=' . $term_ids );
$terms = rtrim( trim( str_replace( '<br />', $separator, $terms ) ), $separator );
// display post categories
echo $terms;
}
?>
<br><span>Episodes:</span>
<?php
$taxonomy = 'episodes';
// get the term IDs assigned to post.
$post_terms = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
// separator between links
$separator = ', ';
if ( !empty( $post_terms ) && !is_wp_error( $post_terms ) ) {
$term_ids = implode( ',' , $post_terms );
$terms = wp_list_categories( 'title_li=&style=none&echo=0&taxonomy=' . $taxonomy . '&include=' . $term_ids );
$terms = rtrim( trim( str_replace( '<br />', $separator, $terms ) ), $separator );
// display post categories
echo $terms;
}
?>
<br><span>Genres:</span>
<?php
$taxonomy = 'genre';
// get the term IDs assigned to post.
$post_terms = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
// separator between links
$separator = ', ';
if ( !empty( $post_terms ) && !is_wp_error( $post_terms ) ) {
$term_ids = implode( ',' , $post_terms );
$terms = wp_list_categories( 'title_li=&style=none&echo=0&taxonomy=' . $taxonomy . '&include=' . $term_ids );
$terms = rtrim( trim( str_replace( '<br />', $separator, $terms ) ), $separator );
// display post categories
echo $terms;
}
?>
<hr>
<?php
endwhile;
}
wp_reset_query(); // Restore global post data stomped by the_post().
?>
Other Information:
Upon clicking the Title of the Show, it'll bring the reader to another page that shows an image of the show, the custom taxonomies listed again, and all other posts related to that Show (connected by Tag being the name of the Show). e.g, If there is a Review or Discussion post tagged with the Show "Boys and Girls", it'll show up on this page.
Upon clicking the taxonomy terms, the reader will be taken to the page that lists all the Shows that are related to that term. e.g, All the shows that have been made by that Studio, all the shows with 12 episodes, all the shows under the Genre 'Action'.
This above can potentially bring into question the overall structure of how I setup this up. I am very new to web development and wordpress, so I have done my best based on the research I've done.
I have a couple of other unanswered Stack Exchange posts that go further into details and questions about structuring this part of the website. Help there is appreciated as well, or if you need further clarification on what I am trying to attempt.
Custom Post Type and Taxonomies Structure
Creating a Sortable Table by Taxonomy Terms
Thank you very much for your time and the help you have all given me so far.
usort
to sort the returned array of results. I have don quite a few on this subject ;-)