Using WordPress ‘add_clean_index()’ PHP function

The add_clean_index() WordPress PHP function is used to add an index to a specified table in a WordPress database. This can help speed up queries involving that table.

Usage

To use the add_clean_index() function, you need to pass in two parameters: the database table name and the index column name. Here’s a custom example:

global $wpdb;
add_clean_index( $wpdb->posts, 'post_name' );

In the above example, an index is added to the ‘post_name’ column of the ‘posts’ table.

Parameters

  • $table (string) – This is the name of the database table to which you want to add an index.
  • $index (string) – This is the name of the column in the table that you want to index.

More information

See WordPress Developer Resources: add_clean_index

Please note that the add_clean_index() function is not a part of the WordPress core and may require specific plugins or custom code to be available. Always check the source code and compatibility before using in your project.

Examples

Add index to ‘post_name’ in ‘posts’ table

This example adds an index to the ‘post_name’ column in the ‘posts’ table.

global $wpdb;
add_clean_index( $wpdb->posts, 'post_name' );

Add index to ‘category_nicename’ in ‘categories’ table

This example adds an index to the ‘category_nicename’ column in the ‘categories’ table.

global $wpdb;
add_clean_index( $wpdb->categories, 'category_nicename' );

Add index to ‘comment_approved’ in ‘comments’ table

This example adds an index to the ‘comment_approved’ column in the ‘comments’ table.

global $wpdb;
add_clean_index( $wpdb->comments, 'comment_approved' );

Add index to ‘post_status’ in ‘posts’ table

This example adds an index to the ‘post_status’ column in the ‘posts’ table.

global $wpdb;
add_clean_index( $wpdb->posts, 'post_status' );

Add multiple indices in one go

This example adds indices to multiple columns across different tables.

global $wpdb;
add_clean_index( $wpdb->posts, 'post_name' );
add_clean_index( $wpdb->categories, 'category_nicename' );
add_clean_index( $wpdb->comments, 'comment_approved' );
add_clean_index( $wpdb->posts, 'post_status' );

In this case, we’re adding indices to ‘post_name’ and ‘post_status’ in the ‘posts’ table, ‘category_nicename’ in the ‘categories’ table, and ‘comment_approved’ in the ‘comments’ table.