By default, the series taxonomy archive is ordered by the meta key “novelist_series” number parameter (the book’s numerical position in the series). The one downside of this is that if the series number is not filled out, the book will not appear in the archive at all. This can be fixed by changing the orderby parameter to something else.
The below example demonstrates how to change the parameter to use the book’s publication date instead:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Modify Book Query | |
| * | |
| * Modifies the WP_Query to change the series archive "orderby" parameter to use | |
| * the book's publication date rather than the number in the series. This | |
| * eliminates the need to fill out the book series number. | |
| * | |
| * @param WP_Query $query | |
| * | |
| * @return void | |
| */ | |
| function ag_novelist_series_archive_orderby( $query ) { | |
| if ( ! $query->is_main_query() || is_admin() ) { | |
| return; | |
| } | |
| if ( is_tax( 'novelist-series' ) ) { | |
| $query->set( 'orderby', 'meta_value_num' ); | |
| $query->set( 'order', 'ASC' ); | |
| $query->set( 'meta_key', 'novelist_pub_date_timestamp' ); | |
| } | |
| } | |
| add_action( 'pre_get_posts', 'ag_novelist_series_archive_orderby', 100 ); |