Make WordPress Core


Ignore:
Timestamp:
02/02/2021 08:32:42 PM (4 years ago)
Author:
whyisjake
Message:

Administration: New filter ahead of the months drop-down.

As this can cause large, long running queries on sites with many posts, this filter allows the query to be modified, bypassing entirely if needed.

Fixes #51660.

Props geoffguillain, SergeyBiryukov, hareesh-pillai, hellofromTonya, TimothyBlynJacobs, whyisjake.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/class-wp-list-table.php

    r50120 r50163  
    596596        }
    597597
    598         $extra_checks = "AND post_status != 'auto-draft'";
    599         if ( ! isset( $_GET['post_status'] ) || 'trash' !== $_GET['post_status'] ) {
    600             $extra_checks .= " AND post_status != 'trash'";
    601         } elseif ( isset( $_GET['post_status'] ) ) {
    602             $extra_checks = $wpdb->prepare( ' AND post_status = %s', $_GET['post_status'] );
    603         }
    604 
    605         $months = $wpdb->get_results(
    606             $wpdb->prepare(
    607                 "
    608             SELECT DISTINCT YEAR( post_date ) AS year, MONTH( post_date ) AS month
    609             FROM $wpdb->posts
    610             WHERE post_type = %s
    611             $extra_checks
    612             ORDER BY post_date DESC
    613         ",
    614                 $post_type
    615             )
    616         );
    617 
    618598        /**
    619          * Filters the 'Months' drop-down results.
     599         * Filters to short-circuit performing the months dropdown query.
    620600         *
    621          * @since 3.7.0
     601         * @since 5.7.0
    622602         *
    623          * @param object[] $months    Array of the months drop-down query results.
    624          * @param string   $post_type The post type.
     603         * @param object[]|false $months   'Months' drop-down results. Default false.
     604         * @param string         $post_type The post type.
    625605         */
    626         $months = apply_filters( 'months_dropdown_results', $months, $post_type );
     606        $months = apply_filters( 'pre_months_dropdown_query', false, $post_type );
     607
     608        if ( ! is_array( $months ) ) {
     609            $extra_checks = "AND post_status != 'auto-draft'";
     610            if ( ! isset( $_GET['post_status'] ) || 'trash' !== $_GET['post_status'] ) {
     611                $extra_checks .= " AND post_status != 'trash'";
     612            } elseif ( isset( $_GET['post_status'] ) ) {
     613                $extra_checks = $wpdb->prepare( ' AND post_status = %s', $_GET['post_status'] );
     614            }
     615
     616            $months = $wpdb->get_results(
     617                $wpdb->prepare(
     618                    "
     619                SELECT DISTINCT YEAR( post_date ) AS year, MONTH( post_date ) AS month
     620                FROM $wpdb->posts
     621                WHERE post_type = %s
     622                $extra_checks
     623                ORDER BY post_date DESC
     624            ",
     625                    $post_type
     626                )
     627            );
     628
     629            /**
     630             * Filters the 'Months' drop-down results.
     631             *
     632             * @since 3.7.0
     633             *
     634             * @param object[] $months    Array of the months drop-down query results.
     635             * @param string   $post_type The post type.
     636             */
     637            $months = apply_filters( 'months_dropdown_results', $months, $post_type );
     638        }
    627639
    628640        $month_count = count( $months );
Note: See TracChangeset for help on using the changeset viewer.