Skip to content

Instantly share code, notes, and snippets.

@orangerdev
Last active October 10, 2017 07:32
Show Gist options
  • Save orangerdev/47602bc632917a079c1511691af68413 to your computer and use it in GitHub Desktop.
Save orangerdev/47602bc632917a079c1511691af68413 to your computer and use it in GitHub Desktop.
TASK 3 - WPI CLI
<?php
namespace WP;
class CLI
{
/**
* Construction
*/
public function __construct()
{
}
/**
* Set true/false field for all company posts
* @param array $args
* 1/0 => true|false
*
*
* EXAMPLES
*
* wp upgrade set 1
* @return void
*/
public function set($args)
{
if(!isset($args[0])) :
\WP_CLI::error(__('Empty upgraded value','CLI'));
return;
endif;
$i = 0;
$posts_per_page = 20;
$offset = 0;
$repeat = true;
$boolean = (1 === $args[0]) ? TRUE : FALSE;
// CHUNK POSTS TO 20
while($repeat) :
$args = [
'posts_per_page' => $posts_per_page,
'post_type' => 'company',
'post_status' => 'publish',
'offset' => $offset
];
$posts = get_posts($args);
if(is_array($posts) && 0 < count($posts)) :
foreach($posts as $post) :
update_field('upgraded',$boolean,$post->ID);
\WP_CLI::success(sprintf(__('Company ID %s set as %s','CLI'),$post->ID,$boolean));
endforeach;
$i++;
$offset = $posts_per_page * $i;
else :
// NOT FOUND ANY POSTS
// STOP THE LOOP
$repeat = false;
endif;
endwhile;
}
}
new CLI;
\WP_CLI::add_command('upgrade' ,'\WP\CLI');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment