-
Notifications
You must be signed in to change notification settings - Fork 0
/
class-clearpostcache-command.php
56 lines (51 loc) · 1.47 KB
/
class-clearpostcache-command.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
/**
* Clear Post Cache
* Sometimes you might need to clean all the post caches. Eg after doing a search and replace. Here's an easy way to do this without wiping the whole memcache.
* @author Allan Collins <[email protected]>
*
*/
class Clearpostcache_Command extends WP_CLI_Command {
/**
* Queries posts, loops through results and then cleans each post cache.
*
* ## OPTIONS
*
* [--<field>=<value>]
* : One or more args to pass to WP_Query.
*
* ## EXAMPLES
*
* wp clearpostcache
* wp clearpostcache --post_type=post
*
* @subcommand clearpostcache [--foo=<bar>]
*/
public function __invoke( $_, $assoc_args ) {
$defaults = array(
'posts_per_page' => 100,
'post_status' => 'any',
'fields' => 'ids',
'paged' => 1
);
$query_args = array_merge( $defaults, $assoc_args );
$query = new WP_Query( $query_args );
$post_ids = $query->posts;
$current_page = $query->get( 'paged' );
$total = $query->max_num_pages;
$notify = \WP_CLI\Utils\make_progress_bar( 'Cleaning Post Caches:', $query->found_posts );
$done = 0;
while ( $current_page <= $total ) {
$query->set( 'paged', $current_page );
$query->get_posts();
foreach ( $query->posts as $id ) {
clean_post_cache( $id );
$notify->tick();
$done++;
}
$current_page++;
}
WP_CLI::success( sprintf( '%d of %d posts flushed.', $done, $query->found_posts ) );
}
}
WP_CLI::add_command( 'clearpostcache', 'Clearpostcache_Command' );