WP_CLI\Iterators
Table::__construct
Creates an iterator over a database table.
foreach( new Iterators\Table( array( 'table' => $wpdb->posts, 'fields' => array( 'ID', 'post_content' ) ) ) as $post ) {
count_words_for( $post->ID, $post->post_content );
}
foreach( new Iterators\Table( array( 'table' => $wpdb->posts, 'where' => 'ID = 8 OR post_status = "publish"' ) ) as $post ) {
…
}
foreach( new PostIterator( array( 'table' => $wpdb->posts, 'where' => array( 'post_status' => 'publish', 'post_date_gmt BETWEEN x AND y' ) ) ) as $post ) {
…
}
Method of the class: Table{}
No Hooks.
Returns
null. Nothing (null).
Usage
$Table = new Table(); $Table->__construct( $args );
- $args(array)
- Supported arguments:
php table – the name of the database table fields – an array of columns to get from the table, '*' is a valid value and the default where – conditions for filtering rows. Supports two formats: = string – this will be the where clause = array – each element is treated as a condition if it's positional, or as column => value if it's a key/value pair. In the latter case the value is automatically quoted and escaped append - add arbitrary extra SQL.
Default: []
Table::__construct() Table:: construct code WP-CLI 2.13.0-alpha
public function __construct( $args = [] ) {
$defaults = [
'fields' => '*',
'where' => [],
'append' => '',
'table' => null,
'chunk_size' => 500,
];
$table = $args['table'];
$args = array_merge( $defaults, $args );
$fields = self::build_fields( $args['fields'] );
$conditions = self::build_where_conditions( $args['where'] );
$where_sql = $conditions ? " WHERE $conditions" : '';
$query = "SELECT $fields FROM `$table` $where_sql {$args['append']}";
parent::__construct( $query, $args['chunk_size'] );
}