WP_Comment_Query::get_comment_ids(): int|array
- Since
- 4.4.0, 6.9.0
- Source
wp-includes/class-wp-comment-query.php:553
Compatibility
- WordPress
- since 6.9.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Return value
int|array- A single count of comment IDs if a count query. An array of comment IDs if a full query.
Performance profile
How much work a call to WP_Comment_Query::get_comment_ids() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Heavy
- Scaling
- Constant
- Instructions
- 289–303
- Plugin surface
- 1 hook
- Called by
- 1
Reaches the database via ->get_var().
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 954.
Third-party callbacks on 'comments_clauses' run inside this call, and their cost is not bounded by anything here.
1 place in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters_ref_array()called directly - sqldatabase query
->get_var()called directly
What one call costs · 2 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Comment_Query::get_comment_ids() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!empty($statuses) && $statuses && empty($value) && empty($approved_clauses) && empty($number) && empty($post_id) && $parent === "" && !isset($value) && empty($post_fields) && !$clauses | 289–299 | wp_parse_list(), strtoupper(), absint(), absint(), absint(), absint(), array_merge(), wp_array_slice_assoc(), array_filter(), compact(), ->get_var() |
!empty($statuses) && $statuses && empty($value) && empty($approved_clauses) && empty($number) && empty($post_id) && $parent === "" && !isset($value) && empty($post_fields) && !$clauses | 293–303 | wp_parse_list(), strtoupper(), absint(), absint(), absint(), absint(), array_merge(), wp_array_slice_assoc(), array_filter(), compact(), ->get_col(), array_map() |
This body has more branch combinations than are worth enumerating, so the table covers the outcomes found first rather than every one that exists.
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 953 | 289–303 | 102 | 1 fewer instruction than PHP 8.5 |
| 8.5 | 954 | 289–303 | 102 | |
| 8.4 | 954 | 289–303 | 102 | 89 fewer instructions than PHP 8.3 |
| 8.3 | 1043 | 301–315 | 102 | |
| 8.2 | 1043 | 301–315 | 102 | 2 more instructions than PHP 8.1 |
| 8.1 | 1041 | 301–315 | 102 | 1 fewer instruction than PHP 7.4 |
| 7.4 | 1042 | 301–315 | 102 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Hooks and filters fired · 1
One hook fires while WP_Comment_Query::get_comment_ids() runs, in this order:
- apply_filters( comments_clauses )filterline 958 (+405 into the body)
Filters the comment query clauses.
Uses · 10
- wp_parse_list()Converts a comma- or space-separated list of scalar values to an array.
- str_contains()Polyfill for `str_contains()` function added in PHP 8.0.
- absint()Converts a value to non-negative integer.
- wp_parse_id_list()Cleans up an array, comma- or space-separated list of IDs.
- wp_array_slice_assoc()Extracts a slice of an array, given a list of keys.
- apply_filters_ref_array()Calls the callback functions that have been added to a filter hook, specifying arguments in an array.
- WP_Comment_Query::parse_orderby()Parse and sanitize 'orderby' keys passed to the comment query.
- WP_Comment_Query::parse_order()Parse an 'order' query variable and cast it to ASC or DESC as necessary.
- WP_Comment_Query::get_search_sql()Used internally to generate an SQL string for searching across multiple columns.
- WP_Date_Query::__construct()Constructor.
Used by · 1
- WP_Comment_Query::get_comments()Get a list of comments matching the query vars.
Source code
protected function get_comment_ids() { global $wpdb; // Assemble clauses related to 'comment_approved'. $approved_clauses = array(); // 'status' accepts an array or a comma-separated string. $status_clauses = array(); $statuses = wp_parse_list( $this->query_vars['status'] ); // Empty 'status' should be interpreted as 'all'. if ( empty( $statuses ) ) { $statuses = array( 'all' ); } // 'any' overrides other statuses. if ( ! in_array( 'any', $statuses, true ) ) { foreach ( $statuses as $status ) { switch ( $status ) { case 'hold': $status_clauses[] = "comment_approved = '0'"; break; case 'approve': $status_clauses[] = "comment_approved = '1'"; break; case 'all': case '': $status_clauses[] = "( comment_approved = '0' OR comment_approved = '1' )"; break; default: $status_clauses[] = $wpdb->prepare( 'comment_approved = %s', $status ); break; } } $approved_clauses[] = '( ' . implode( ' OR ', $status_clauses ) . ' )'; } // User IDs or emails whose unapproved comments are included, regardless of $status. if ( ! empty( $this->query_vars['include_unapproved'] ) ) { $include_unapproved = wp_parse_list( $this->query_vars['include_unapproved'] ); foreach ( $include_unapproved as $unapproved_identifier ) { // Numeric values are assumed to be user IDs. if ( is_numeric( $unapproved_identifier ) ) { $approved_clauses[] = $wpdb->prepare( "( user_id = %d AND comment_approved = '0' )", $unapproved_identifier ); } else { // Otherwise we match against email addresses. if ( ! empty( $_GET['unapproved'] ) && ! empty( $_GET['moderation-hash'] ) ) { // Only include requested comment. $approved_clauses[] = $wpdb->prepare( "( comment_author_email = %s AND comment_approved = '0' AND {$wpdb->comments}.comment_ID = %d )", $unapproved_identifier, (int) $_GET['unapproved'] ); } else { // Include all of the author's unapproved comments. $approved_clauses[] = $wpdb->prepare( "( comment_author_email = %s AND comment_approved = '0' )", $unapproved_identifier ); } } } } // Collapse comment_approved clauses into a single OR-separated clause. if ( ! empty( $approved_clauses ) ) { if ( 1 === count( $approved_clauses ) ) { $this->sql_clauses['where']['approved'] = $approved_clauses[0]; } else { $this->sql_clauses['where']['approved'] = '( ' . implode( ' OR ', $approved_clauses ) . ' )'; } } $order = ( 'ASC' === strtoupper( $this->query_vars['order'] ) ) ? 'ASC' : 'DESC'; // Disable ORDER BY with 'none', an empty array, or boolean false. if ( in_array( $this->query_vars['orderby'], array( 'none', array(), false ), true ) ) { $orderby = ''; } elseif ( ! empty( $this->query_vars['orderby'] ) ) { $ordersby = is_array( $this->query_vars['orderby'] ) ? $this->query_vars['orderby'] : preg_split( '/[,\s]/', $this->query_vars['orderby'] );Changelog
Introduced in 4.4.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-includes/class-wp-comment-query.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.