@@ -31,57 +31,76 @@ class CLI extends WP_CLI_Command {
3131 * Indexes all posts
3232 *
3333 * ## OPTIONS
34+ * [--post-type=<name>]
35+ * : post type, 'any' if not defined
3436 *
3537 * ## EXAMPLES
3638 *
3739 * wp facet index
40+ * wp facet index --post-type=product
3841 *
3942 * @synopsis
4043 */
4144 function index ( $ args , $ assoc_args ) {
4245
4346 error_reporting (0 );
4447
45- $ post_type = 'any ' ;
46-
47- if ( isset ( $ args [0 ] ) ) {
48- $ post_type = $ args [0 ];
49- }
48+ if ( empty ( $ assoc_args ['post-type ' ] ) )
49+ $ post_type = 'any ' ;
50+ else
51+ $ post_type = $ assoc_args ['post-type ' ];
5052
5153 $ posts_per_page = 100 ;
5254 $ page = 1 ;
5355
54- do {
55- $ post_ids = get_posts ( array (
56+ $ args = array (
5657 'posts_per_page ' => $ posts_per_page ,
5758 'paged ' => $ page ,
5859 'post_type ' => $ post_type ,
5960 'post_status ' => 'publish ' ,
6061 'fields ' => 'ids ' ,
6162 'orderby ' => 'ID ' ,
6263 'cache_results ' => false ,
63- ));
64-
65- // Do stuff
66-
67- $ progress_bar = WP_CLI \Utils \make_progress_bar ('Indexing ' , count ( $ post_ids ));
68-
69- $ indexer = new FacetWP_Indexer ;
70- $ indexer ->is_overridden = true ;
64+ );
7165
72- foreach ( $ post_ids as $ post_id ){
73- $ progress_bar ->tick ();
74- $ indexer ->index ( $ post_id );
75- }
66+ $ total = 0 ;
7667
77- $ progress_bar ->finish ();
78-
79- $ page ++;
80-
81- // Free up memory
82- $ this ->stop_the_insanity ();
83-
84- } while ( count ( $ post_ids ) );
68+ do {
69+ $ args ['paged ' ] = $ page ;
70+ $ my_query = new WP_Query ( $ args );
71+
72+ if ($ my_query ->have_posts ())
73+ {
74+ if ($ page == 1 )
75+ {
76+ $ total = $ my_query ->found_posts ;
77+ WP_CLI ::line ( 'Found ' .$ total .' posts of type " ' .$ post_type .'" ' );
78+ $ progress_bar = WP_CLI \Utils \make_progress_bar ('Indexing ' , $ total );
79+ }
80+
81+ foreach ( $ my_query ->posts as $ key => $ post_id )
82+ {
83+ //WP_CLI::line( $post_id );
84+ $ progress_bar ->tick ();
85+ FWP ()->indexer ->index ( $ post_id );
86+ }
87+
88+ // Free up memory
89+ $ this ->stop_the_insanity ();
90+
91+ $ page ++;
92+ }
93+
94+
95+ } while ( $ my_query ->have_posts () );
96+
97+ if ($ total > 0 )
98+ {
99+ $ progress_bar ->finish ();
100+ WP_CLI ::success ( 'All posts indexed ' );
101+ } else {
102+ WP_CLI ::error ( 'No posts of type " ' .$ post_type .'" found! ' );
103+ }
85104 }
86105
87106 /*
@@ -102,5 +121,4 @@ protected function stop_the_insanity() {
102121
103122}
104123
105-
106124WP_CLI ::add_command ( 'facet ' , __NAMESPACE__ . '\\CLI ' );
0 commit comments