Remember how I talked about doing math on a post when it was saved?
Well what if I wanted to run that at another time? Like what if I knew I needed to update one show and I didn’t want to go in and save the post?
I can to this with WP-CLI:
WP CLI Magic
class WP_CLI_MySite_Commands extends WP_CLI_Command {
/**
* Re-run calculations for specific post content.
*
* ## EXAMPLES
*
* wp mysite calc actor ID
* wp mysite calc show ID
*
*/
function calc( $args , $assoc_args ) {
// Valid things to calculate:
$valid_calcs = array( 'actor', 'show' );
// Defaults
$format = ( isset( $assoc_args['format'] ) )? $assoc_args['format'] : 'table';
// Check for valid arguments and post types
if ( empty( $args ) || !in_array( $args[0], $valid_calcs ) ) {
WP_CLI::error( 'You must provide a valid type of calculation to run: ' . implode( ', ', $valid_calcs ) );
}
// Check for valid IDs
if( empty( $args[1] ) || !is_numeric( $args[1] ) ) {
WP_CLI::error( 'You must provide a valid post ID to calculate.' );
}
// Set the post IDs:
$post_calc = sanitize_text_field( $args[0] );
$post_id = (int)$args[1];
// Last sanitity check: Is the post ID a member of THIS post type...
if ( get_post_type( $post_id ) !== 'post_type_' . $post_calc . 's' ) {
WP_CLI::error( 'You can only calculate ' . $post_type . 's on ' . $post_type . ' pages.' );
}
// Do the thing!
// i.e. run the calculations
switch( $post_calc ) {
case 'show':
// Rerun show calculations
MySite_Show_Calculate::do_the_math( $post_id );
$score = 'Score: ' . get_post_meta( $post_id, 'shows_the_score', true );
break;
case 'actor':
// Recount characters and flag queerness
MySite_Actor_Calculate::do_the_math( $post_id );
$queer = ( get_post_meta( $post_id, 'actors_queer', true ) )? 'Yes' : 'No';
$chars = get_post_meta( $post_id, 'actors_char_count', true );
$deads = get_post_meta( $post_id, 'actors_dead_count', true );
$score = ': Is Queer (' . $queer . ') Chars (' . $chars . ') Dead (' . $deads . ')';
break;
}
WP_CLI::success( 'Calculations run for ' . get_the_title( $post_id ) . $score );
}
}
What the What?
The one for shows is a lot simpler. I literally call that do_the_math() function with the post ID and I get back a number. Then I output the number and I’m done. If I wanted it to run for all shows, I could use WP-CLI to spit out a list of all the IDs and then pass them to the command one at a time. Or I could write one that does ‘all’ posts. Which I may
But the point is that I now can type wp mysite calc show 1234 and if post ID 1234 is a show, it’ll run.

