Changeset 9317
- Timestamp:
- 12/10/2019 07:48:38 PM (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-developer/inc/search.php
r9316 r9317 25 25 add_action( 'pre_get_posts', array( __CLASS__, 'pre_get_posts' ), 20 ); 26 26 add_filter( 'posts_orderby', array( __CLASS__, 'search_posts_orderby' ), 10, 2 ); 27 add_filter( 'the_posts', array( __CLASS__, 'redirect_empty_search' ), 10, 2 ); 27 28 add_filter( 'the_posts', array( __CLASS__, 'rerun_empty_search' ), 10, 2 ); 28 29 add_action( 'wp_head', array( __CLASS__, 'noindex_for_search' ), 9 ); … … 200 201 201 202 /** 203 * Redirects empty searches. 204 * 205 * @access public 206 * 207 * @param array $posts Array of posts after the main query 208 * @param WP_Query $query WP_Query object 209 * @return array 210 * 211 */ 212 public static function redirect_empty_search( $posts, $query ) { 213 $redirect = ''; 214 215 // If request is an empty search. 216 if ( $query->is_main_query() && $query->is_search() && ! trim( get_search_query() ) ) { 217 // If search is filtered. 218 if ( isset( $_GET['post_type'] ) ) { 219 $post_types = $_GET['post_type']; 220 221 // Redirect to post type archive if only a single parsed post type is defined. 222 if ( 1 === count( $post_types ) ) { 223 // Note: By this point, via `invalid_post_type_filter_404()`, we know 224 // the post type is valid. 225 $redirect = get_post_type_archive_link( $post_types[0] ); 226 } 227 // Otherwise, redirect to Code Reference landing page. 228 else { 229 $redirect = home_url( '/reference' ); 230 } 231 } 232 // Else search is unfiltered, so redirect to Code Reference landing page. 233 else { 234 $redirect = home_url( '/reference' ); 235 } 236 } 237 238 // Empty unfiltered search should redirect to Code Reference landing page. 239 if ( $redirect ) { 240 wp_safe_redirect( $redirect ); 241 exit; 242 } 243 244 return $posts; 245 } 246 247 /** 202 248 * Potentially rerun a search if no posts were found. 203 249 *
Note: See TracChangeset
for help on using the changeset viewer.