Skip to content

Instantly share code, notes, and snippets.

@spacedmonkey
Created October 6, 2014 19:09
Show Gist options
  • Save spacedmonkey/0409ac984a58b2bf159b to your computer and use it in GitHub Desktop.
Save spacedmonkey/0409ac984a58b2bf159b to your computer and use it in GitHub Desktop.
Post Terms Command
<?php
/**
* Manage posts terms.
*
* @package wp-cli
*/
class Post_Terms_Command extends \WP_CLI\CommandWithDBObject {
protected $obj_type = 'post terms for';
public function __construct() {
$this->fetcher = new \WP_CLI\Fetchers\Post;
}
/**
* Get a terms of linked to a post.
*
* <id>...
* : ID of posts
*
* [--taxonomy=<taxonomy>]
* : Only show term for this taxonomy
*
* ## EXAMPLES
*
* # save the post content to a file
* wp post terms list 12
*
* @subcommand list
*/
public function list_( $args, $assoc_args ) {
$post = $this->fetcher->get_check( $args[0] );
if(isset($assoc_args['taxonomy'])){
$this->taxonomy_exists($assoc_args['taxonomy']);
$taxonomy_names = array($assoc_args['taxonomy']);
}else{
$taxonomy_names = get_object_taxonomies( $post->post_type );
}
if(empty($taxonomy_names)){
WP_CLI::warning(__('No taxonomies found'));
return;
}
$display = array();
foreach($taxonomy_names as $taxonomy_name){
$term_list = wp_get_post_terms($args[0], $taxonomy_name);
foreach($term_list as $term){
$display[] = (object) array('Term ID' => $term->term_id, 'Name' => $term->name, 'Slug' => $term->slug, 'Taxonomy' => $term->taxonomy);
}
}
if(empty($display)){
WP_CLI::warning(__('No terms found'));
return;
}
$fields = array('Term ID', 'Name', 'Slug', 'Taxonomy' );
\WP_CLI\Utils\format_items('table', $display, $fields);
}
/**
* Add terms one or more posts.
*
* ## OPTIONS
*
* <id>...
* : One or more IDs of posts to update.
*
*
* --taxonomy=<taxonomy>
* : One or more fields to update. See wp_update_post().
*
* --term=<term>
* : One or more fields to update. See wp_update_post().
*
* ## EXAMPLES
*
* wp post terms add 123
*/
public function add( $args, $assoc_args ) {
$args = $this->processIds($args);
parent::_update( $args, $assoc_args, function ( $params ) {
$this->addterms($params['ID'], $params['term'],$params['taxonomy'], false);
} );
}
/**
* Update terms on one or more posts.
*
* ## OPTIONS
*
* <id>...
* : One or more IDs of posts to update.
*
* --taxonomy=<taxonomy>
* : One or more fields to update. See wp_update_post().
*
* --term=<term>
* : One or more fields to update. See wp_update_post().
*
* ## EXAMPLES
*
* wp post terms edit 123
*/
public function update( $args, $assoc_args ) {
$args = $this->processIds($args);
parent::_update( $args, $assoc_args, function ( $params ) {
$this->addterms($params['ID'], $params['term'],$params['taxonomy'], true);
} );
}
/**
* Remove term from one or more posts.
*
* ## OPTIONS
*
* <id>...
* : One or more IDs of posts to update.
*
* --taxonomy=<taxonomy>
* : One or more fields to update. See wp_update_post().
*
* --term=<term>
* : One or more fields to update. See wp_update_post().
*
* ## EXAMPLES
*
* wp post terms delete 123
*/
public function delete( $args, $assoc_args ) {
$args = $this->processIds($args);
parent::_delete( $args, $assoc_args, function ( $id, $params ) {
$r = wp_remove_object_terms( $id, $params['term'],$params['taxonomy'] );
return $this->wp_error_to_resp($r, sprintf(__('Removed the term %s from %s'),$params['term'],$id));
} );
}
protected function processIds($args){
foreach( $args as $key => $arg ) {
if ( is_numeric( $arg ) ) {
continue;
}
unset( $args[ $key ] );
break;
}
return $args;
}
protected function addterms($post_id, $terms, $taxonomy, $append = true){
$post = $this->fetcher->get_check( $post_id );
return wp_set_object_terms( $post->ID, $terms, $taxonomy, $append );
}
protected function taxonomy_exists($taxonomy){
if(!taxonomy_exists($taxonomy)){
WP_CLI::error(__('Invalid taxonomy'));
}
}
}
WP_CLI::add_command( 'post terms', 'Post_Terms_Command' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment