Making WordPress.org

Changeset 8288


Ignore:
Timestamp:
02/20/2019 04:08:04 AM (6 years ago)
Author:
dd32
Message:

Make Theme: Ensure all searches and empty archives load the 404 template, and 404 correctly.

This is a bit funky due to some oddities in the theme template, site content, and meeting archives.

Fixes #4187.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-makehome/functions.php

    r7795 r8288  
    1515add_action( 'pre_get_posts', 'make_query_mods' );
    1616function make_query_mods( $query ) {
    17     if ( ! is_admin() && $query->is_main_query() && $query->is_home() )
     17    if ( ! is_admin() && $query->is_main_query() && $query->is_home() ) {
    1818        $query->set( 'posts_per_page', 1 );
     19    }
     20
     21    // There's nothing worth searching for on this site.
     22    if ( ! is_admin() && $query->is_main_query() && $query->is_search() ) {
     23        $query->set_404();
     24    }
    1925}
     26
     27add_filter( 'the_posts', function( $posts, $query ) {
     28    // Ensure all non-post routes 404, as this site isn't like most others.
     29    if (
     30        ( ! is_admin() && $query->is_main_query() && ! $posts ) ||
     31        ( ! is_admin() && $query->is_main_query() && $query->is_post_type_archive( 'meeting' ) && $query->get('paged') > 1 ) // Pagination on the query is explicitly disabled, so this doens't 404
     32    ) {
     33        $query->set_404();
     34        status_header( 404 );
     35        nocache_headers();
     36    }
     37
     38    return $posts;
     39}, 10, 2 );
    2040
    2141add_filter('post_class','make_home_site_classes', 10, 3);
Note: See TracChangeset for help on using the changeset viewer.