Making WordPress.org

Changeset 9317


Ignore:
Timestamp:
12/10/2019 07:48:38 PM (4 years ago)
Author:
coffee2code
Message:

Developer: Redirect empty Code Reference searches back to its landing page.

Though if the empty search was for just one of the filterable types, go to that type's archive page instead.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-developer/inc/search.php

    r9316 r9317  
    2525        add_action( 'pre_get_posts', array( __CLASS__, 'pre_get_posts' ), 20 );
    2626        add_filter( 'posts_orderby', array( __CLASS__, 'search_posts_orderby' ), 10, 2 );
     27        add_filter( 'the_posts',     array( __CLASS__, 'redirect_empty_search' ), 10, 2 );
    2728        add_filter( 'the_posts',     array( __CLASS__, 'rerun_empty_search' ), 10, 2 );
    2829        add_action( 'wp_head',       array( __CLASS__, 'noindex_for_search' ), 9 );
     
    200201
    201202    /**
     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    /**
    202248     * Potentially rerun a search if no posts were found.
    203249     *
Note: See TracChangeset for help on using the changeset viewer.