Making WordPress.org

Changeset 14970


Ignore:
Timestamp:
07/14/2026 08:54:21 PM (11 days ago)
Author:
obenland
Message:

Theme Directory: Restrict search results to published themes

Props obenland, stiofansisland, dd32, dufresnesteven, ryelle.
Closes https://github.com/WordPress/wordpress.org/pull/712.

Location:
sites/trunk/wordpress.org/public_html/wp-content/plugins/theme-directory
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/theme-directory/query-modifications.php

    r13703 r14970  
    189189
    190190/**
     191 * Restricts Jetpack Search (Elasticsearch) theme searches to published themes.
     192 *
     193 * Without this, the ES hit total counts themes in non-public statuses that are
     194 * then dropped when the results are loaded for display, leaving search pages
     195 * with fewer cards than the total implies.
     196 *
     197 * @param array    $es_query_args The raw Elasticsearch query args.
     198 * @param WP_Query $query         The originating WP_Query object.
     199 * @return array
     200 */
     201function wporg_themes_restrict_search_to_published( $es_query_args, $query ) {
     202        if ( ! $query instanceof WP_Query || 'repopackage' !== $query->get( 'post_type' ) ) {
     203                return $es_query_args;
     204        }
     205
     206        $status_filter = array(
     207                'terms' => array(
     208                        'post_status' => array( 'publish' ),
     209                ),
     210        );
     211
     212        // Merge into the filter tree, matching Jetpack's `and` grouping.
     213        if ( empty( $es_query_args['filter'] ) ) {
     214                $es_query_args['filter'] = $status_filter;
     215        } elseif ( isset( $es_query_args['filter']['and'] ) ) {
     216                $es_query_args['filter']['and'][] = $status_filter;
     217        } else {
     218                $es_query_args['filter'] = array(
     219                        'and' => array( $es_query_args['filter'], $status_filter ),
     220                );
     221        }
     222
     223        return $es_query_args;
     224}
     225add_filter( 'jetpack_search_es_query_args', 'wporg_themes_restrict_search_to_published', 10, 2 );
     226
     227/**
    191228 * Filters SQL clauses, to prioritize translated themes.
    192229 *
Note: See TracChangeset for help on using the changeset viewer.