Created
October 25, 2013 05:11
-
-
Save felipelavinz/7149681 to your computer and use it in GitHub Desktop.
post-term commands for WP-CLI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Set or explore post term associations | |
* | |
* @author Felipe Lavín Z. <[email protected]> | |
*/ | |
class Post_Term extends \WP_CLI_Command{ | |
/** | |
* Add a term to a post | |
* | |
* @synopsis <id> <taxonomy> [--slug=<slug>] [--term-id=<term-id>] [--replace] | |
*/ | |
public function add( $args, $assoc_args ){ | |
if ( empty($assoc_args['slug']) && empty($assoc_args['term-id']) ) { | |
WP_CLI::error("You must specify the term slug or the term_id"); | |
} | |
if ( ! empty($assoc_args['term-id']) ) { | |
$term = \absint( $assoc_args['term-id'] ); | |
} else { | |
$term = $assoc_args['slug']; | |
} | |
list( $post_id, $taxonomy ) = $args; | |
if ( !isset($post_id) ){ | |
WP_CLI::error("You must specify the post ID"); | |
} | |
if ( !isset($taxonomy) ){ | |
WP_CLI::error("You must specify the term taxonomy"); | |
} | |
// if replace is set, then append is false | |
$append = !! empty( $assoc_args['replace'] ); | |
$set_term = \wp_set_object_terms( $post_id, $term, $taxonomy, $append ); | |
if ( \is_wp_error($success) ) { | |
WP_CLI::warning( $success->get_error_mesage() ); | |
} else { | |
WP_CLI::success("Term $term successfully added to post ID $post_id"); | |
} | |
} | |
public function delete( $args, $assoc_args ){ | |
} | |
public function get( $args, $assoc_args ){ | |
} | |
public function update( $args, $assoc_args ){ | |
} | |
} | |
WP_CLI::add_command( 'post-term', 'Post_Term' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment