Title: WP_Comments_List_Table::prepare_items
Published: April 25, 2014
Last modified: February 24, 2026

---

# WP_Comments_List_Table::prepare_items()

## In this article

 * [Source](https://developer.wordpress.org/reference/classes/wp_comments_list_table/prepare_items/?output_format=md#source)
 * [Hooks](https://developer.wordpress.org/reference/classes/wp_comments_list_table/prepare_items/?output_format=md#hooks)
 * [Related](https://developer.wordpress.org/reference/classes/wp_comments_list_table/prepare_items/?output_format=md#related)

[ Back to top](https://developer.wordpress.org/reference/classes/wp_comments_list_table/prepare_items/?output_format=md#wp--skip-link--target)

## 󠀁[Source](https://developer.wordpress.org/reference/classes/wp_comments_list_table/prepare_items/?output_format=md#source)󠁿

    ```php
    public function prepare_items() {
    	global $mode, $post_id, $comment_status, $comment_type, $search;

    	if ( ! empty( $_REQUEST['mode'] ) ) {
    		$mode = 'excerpt' === $_REQUEST['mode'] ? 'excerpt' : 'list';
    		set_user_setting( 'posts_list_mode', $mode );
    	} else {
    		$mode = get_user_setting( 'posts_list_mode', 'list' );
    	}

    	$comment_status = isset( $_REQUEST['comment_status'] ) ? $_REQUEST['comment_status'] : 'all';

    	if ( ! in_array( $comment_status, array( 'all', 'mine', 'moderated', 'approved', 'spam', 'trash' ), true ) ) {
    		$comment_status = 'all';
    	}

    	$comment_type = '';

    	if ( ! empty( $_REQUEST['comment_type'] ) && 'note' !== $_REQUEST['comment_type'] ) {
    		$comment_type = $_REQUEST['comment_type'];
    	}

    	$search = ( isset( $_REQUEST['s'] ) ) ? $_REQUEST['s'] : '';

    	$post_type = ( isset( $_REQUEST['post_type'] ) ) ? sanitize_key( $_REQUEST['post_type'] ) : '';

    	$user_id = ( isset( $_REQUEST['user_id'] ) ) ? $_REQUEST['user_id'] : '';

    	$orderby = ( isset( $_REQUEST['orderby'] ) ) ? $_REQUEST['orderby'] : '';
    	$order   = ( isset( $_REQUEST['order'] ) ) ? $_REQUEST['order'] : '';

    	$comments_per_page = $this->get_per_page( $comment_status );

    	$doing_ajax = wp_doing_ajax();

    	if ( isset( $_REQUEST['number'] ) ) {
    		$number = (int) $_REQUEST['number'];
    	} else {
    		$number = $comments_per_page + min( 8, $comments_per_page ); // Grab a few extra.
    	}

    	$page = $this->get_pagenum();

    	if ( isset( $_REQUEST['start'] ) ) {
    		$start = $_REQUEST['start'];
    	} else {
    		$start = ( $page - 1 ) * $comments_per_page;
    	}

    	if ( $doing_ajax && isset( $_REQUEST['offset'] ) ) {
    		$start += $_REQUEST['offset'];
    	}

    	$status_map = array(
    		'mine'      => '',
    		'moderated' => 'hold',
    		'approved'  => 'approve',
    		'all'       => '',
    	);

    	$args = array(
    		'status'                    => isset( $status_map[ $comment_status ] ) ? $status_map[ $comment_status ] : $comment_status,
    		'search'                    => $search,
    		'user_id'                   => $user_id,
    		'offset'                    => $start,
    		'number'                    => $number,
    		'post_id'                   => $post_id,
    		'type'                      => $comment_type,
    		'type__not_in'              => array( 'note' ),
    		'orderby'                   => $orderby,
    		'order'                     => $order,
    		'post_type'                 => $post_type,
    		'update_comment_post_cache' => true,
    	);

    	/**
    	 * Filters the arguments for the comment query in the comments list table.
    	 *
    	 * @since 5.1.0
    	 *
    	 * @param array $args An array of get_comments() arguments.
    	 */
    	$args = apply_filters( 'comments_list_table_query_args', $args );

    	$_comments = get_comments( $args );

    	if ( is_array( $_comments ) ) {
    		$this->items       = array_slice( $_comments, 0, $comments_per_page );
    		$this->extra_items = array_slice( $_comments, $comments_per_page );

    		$_comment_post_ids = array_unique( wp_list_pluck( $_comments, 'comment_post_ID' ) );

    		$this->pending_count = get_pending_comments_num( $_comment_post_ids );
    	}

    	$total_comments = get_comments(
    		array_merge(
    			$args,
    			array(
    				'count'   => true,
    				'offset'  => 0,
    				'number'  => 0,
    				'orderby' => 'none',
    			)
    		)
    	);

    	$this->set_pagination_args(
    		array(
    			'total_items' => $total_comments,
    			'per_page'    => $comments_per_page,
    		)
    	);
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-admin/includes/class-wp-comments-list-table.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-admin/includes/class-wp-comments-list-table.php#L86)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-admin/includes/class-wp-comments-list-table.php#L86-L199)

## 󠀁[Hooks](https://developer.wordpress.org/reference/classes/wp_comments_list_table/prepare_items/?output_format=md#hooks)󠁿

 [apply_filters( ‘comments_list_table_query_args’, array $args )](https://developer.wordpress.org/reference/hooks/comments_list_table_query_args/)

Filters the arguments for the comment query in the comments list table.

## 󠀁[Related](https://developer.wordpress.org/reference/classes/wp_comments_list_table/prepare_items/?output_format=md#related)󠁿

| Uses | Description | 
| [wp_doing_ajax()](https://developer.wordpress.org/reference/functions/wp_doing_ajax/)`wp-includes/load.php` |

Determines whether the current request is a WordPress Ajax request.

  | 
| [WP_Comments_List_Table::get_per_page()](https://developer.wordpress.org/reference/classes/wp_comments_list_table/get_per_page/)`wp-admin/includes/class-wp-comments-list-table.php` |  | 
| [get_pending_comments_num()](https://developer.wordpress.org/reference/functions/get_pending_comments_num/)`wp-admin/includes/comment.php` |

Gets the number of pending comments on a post or posts.

  | 
| [wp_list_pluck()](https://developer.wordpress.org/reference/functions/wp_list_pluck/)`wp-includes/functions.php` |

Plucks a certain field out of each object or array in an array.

  | 
| [set_user_setting()](https://developer.wordpress.org/reference/functions/set_user_setting/)`wp-includes/option.php` |

Adds or updates user interface setting.

  | 
| [get_user_setting()](https://developer.wordpress.org/reference/functions/get_user_setting/)`wp-includes/option.php` |

Retrieves user interface setting value based on setting name.

  | 
| [get_comments()](https://developer.wordpress.org/reference/functions/get_comments/)`wp-includes/comment.php` |

Retrieves a list of comments.

  | 
| [sanitize_key()](https://developer.wordpress.org/reference/functions/sanitize_key/)`wp-includes/formatting.php` |

Sanitizes a string key.

  | 
| [apply_filters()](https://developer.wordpress.org/reference/functions/apply_filters/)`wp-includes/plugin.php` |

Calls the callback functions that have been added to a filter hook.

  |

[Show 4 more](https://developer.wordpress.org/reference/classes/wp_comments_list_table/prepare_items/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/classes/wp_comments_list_table/prepare_items/?output_format=md#)

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fclasses%2Fwp_comments_list_table%2Fprepare_items%2F)
before being able to contribute a note or feedback.