Make WordPress Core


Ignore:
Timestamp:
07/01/2017 11:24:26 AM (9 years ago)
Author:
boonebgorges
Message:

Introduce $comment_count param for WP_Query.

This parameter allows querying for posts with a specific value of
comment_count. It is also possible to query for posts that match
a comment_count comparison by passing an array with 'value' and
'compare' operators (eg array( 'compare' => '>', 'value' => 5 )).

Props ramon fincken.
Fixes #28399.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-query.php

    r40972 r40978  
    642642         *              Introduced `RAND(x)` syntax for `$orderby`, which allows an integer seed value to random sorts.
    643643         * @since 4.6.0 Added 'post_name__in' support for `$orderby`. Introduced the `$lazy_load_term_meta` argument.
     644         * @since 4.9.0 Introduced the `$comment_count` parameter.
    644645         * @access public
    645646         *
    … …  
    658659         *     @type array        $category__not_in        An array of category IDs (NOT in).
    659660         *     @type string       $category_name           Use category slug (not name, this or any children).
     661         *     @type array|int    $comment_count           Filter results by comment count. Provide an integer to match
     662         *                                                 comment count exactly. Provide an array with integer 'value'
     663         *                                                 and 'compare' operator ('=', '!=', '>', '>=', '<', '<=' ) to
     664         *                                                 compare against comment_count in a specific way.
    660665         *     @type string       $comment_status          Comment status.
    661666         *     @type int          $comments_per_page       The number of comments to return per page.
    … …  
    21412146                }
    21422147
     2148                // Matching by comment count.
     2149                if ( isset( $q['comment_count'] ) ) {
     2150                        // Numeric comment count is converted to array format.
     2151                        if ( is_numeric( $q['comment_count'] ) ) {
     2152                                $q['comment_count'] = array(
     2153                                        'value' => intval( $q['comment_count'] ),
     2154                                );
     2155                        }
     2156
     2157                        if ( isset( $q['comment_count']['value'] ) ) {
     2158                                $q['comment_count'] = array_merge( array(
     2159                                        'compare' => '=',
     2160                                ), $q['comment_count'] );
     2161
     2162                                // Fallback for invalid compare operators is '='.
     2163                                $compare_operators = array( '=', '!=', '>', '>=', '<', '<=' );
     2164                                if ( ! in_array( $q['comment_count']['compare'], $compare_operators, true ) ) {
     2165                                        $q['comment_count']['compare'] = '=';
     2166                                }
     2167
     2168                                $where .= $wpdb->prepare( " AND {$wpdb->posts}.comment_count {$q['comment_count']['compare']} %d", $q['comment_count']['value'] );
     2169                        }
     2170                }
     2171
    21432172                // MIME-Type stuff for attachment browsing
    21442173
Note: See TracChangeset for help on using the changeset viewer.