Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* REORDER POST NAV */
- add_filter( 'get_next_post_where', 'lbardugo_adjacent_post_where', 10, 5 );
- add_filter( 'get_previous_post_where', 'lbardugo_adjacent_post_where', 10, 5 );
- /**
- * Overrides the post navigation to sort by menu order.
- *
- * @see https://wordpress.stackexchange.com/a/73194/6477
- *
- * @since 1.0.0
- *
- * @param string $where The `WHERE` clause in the SQL.
- * @param bool $in_same_term Whether post should be in the same taxonomy term.
- * @param int[]|string $excluded_terms Array of excluded term IDs. Empty string if none were provided.
- * @param string $taxonomy Taxonomy. Used to identify the term used when `$in_same_term` is true.
- * @param WP_Post $post WP_Post object.
- * @return string
- */
- function lbardugo_adjacent_post_where($where, $in_same_term, $excluded_terms, $taxonomy, $post ) {
- // Get out now if this isn't the query we want to filter
- if ( !is_main_query() || !is_singular() ) {
- return $where;
- }
- // Check if filtering the next or previous clause.
- // By default, sql is WHERE p.post_date > $date for next and < for previous.
- // To sort by menu order ASC then we need to switch this.
- if (strpos(current_filter(), 'next') !== false) {
- $where = str_replace(">", "<", $where);
- } else {
- $where = str_replace("<", ">", $where);
- }
- $patterns = array();
- $patterns[] = '/post_date/';
- $patterns[] = '/\'[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}\'/';
- $replacements = array();
- $replacements[] = 'menu_order';
- $replacements[] = $post->menu_order;
- return preg_replace( $patterns, $replacements, $where );
- }
- add_filter( 'get_next_post_sort', 'lbardugo_adjacent_post_sort', 10, 3 );
- add_filter( 'get_previous_post_sort', 'lbardugo_adjacent_post_sort', 10, 3 );
- /**
- * Overrides the post navigation to sort by menu order.
- *
- * @see https://github.com/WordPress/wordpress-develop/blob/6.4/src/wp-includes/link-template.php#L1988
- *
- * @since 1.0.0
- *
- * @param string $order_by The `ORDER BY` clause in the SQL.
- * @param WP_Post $post WP_Post object.
- * @param string $order Sort order. 'DESC' for previous post, 'ASC' for next.
- * @return string
- */
- function lbardugo_adjacent_post_sort($order_by, $post, $order) {
- // Get out now if this isn't the query we want to filter
- if ( !is_main_query() || !is_singular() ) {
- return $order_by;
- }
- // Check if filtering the next or previous clause.
- if (strpos(current_filter(), 'next') !== false) {
- $order_by = str_replace("ASC", "DESC", $order_by);
- } else {
- $order_by = str_replace("DESC", "ASC", $order_by);
- }
- $pattern = '/post_date/';
- $replacement = 'menu_order';
- return preg_replace( $pattern, $replacement, $order_by );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement