From 1c81aecbbd1341f6e03ecfe3a2332162802501da Mon Sep 17 00:00:00 2001 From: Pea Date: Mon, 22 May 2017 16:06:57 -0400 Subject: [PATCH] Added utility function #20 Added utility function to switch post_type `tni_switch_post_type()` --- includes/utilities.php | 85 ++++++++++++++++++++++++++++++++++++++ readme.txt | 5 ++- tni-core-functionality.php | 4 +- 3 files changed, 91 insertions(+), 3 deletions(-) diff --git a/includes/utilities.php b/includes/utilities.php index 5847022..1be2dcb 100644 --- a/includes/utilities.php +++ b/includes/utilities.php @@ -93,3 +93,88 @@ function tni_core_assign_guest_author_roles() { echo "DONE"; } + +/** + * Switch Post Type + * Change post type and taxonomy term, based on any valid WP_Query arguments + * Can be run using WP-CLI `wp eval 'tni_switch_post_type();'` + * example: `wp eval 'tni_switch_post_type( array( "post_type" => "blogs", "tax_query" => array( array( "taxonomy" => "blog-types", "field" => "term_id", "terms" => 3045 ) ) ), "post", 3049 );'` + + * + * @uses set_post_type() + * @uses wp_set_post_terms() + * + * @since 1.2.5 + * + * @param array $find array of WP_Query arguments + * @param string $new_post_type + * @param int $new_term + * @param string $new_taxonomy + * @return void + */ +function tni_switch_post_type( $old_post_type, $old_term = null, $old_taxonomy = null, $new_post_type, $new_term = null, $new_taxonomy = null ) { + $new_term = (int) $new_term; + + echo "START\n"; + + if( !post_type_exists( $new_post_type ) ) { + echo "The post type $new_post_type does not exist"; + return new WP_Error( 'post_type-invalid', __( "The post type {$new_post_type} does not exist", 'tni-core' ) ); + } + + /* Base query */ + $args = array( + 'post_type' => $old_post_type, + 'posts_per_page' => -1 + ); + + /* If the ter doesn't exist, return an error */ + if( ( $old_term ) && !term_exists( $old_term, $old_taxonomy ) ) { + echo "The taxonomy term $old_term does not exist"; + return new WP_Error( 'term-invalid', __( "The taxonomy term {$old_term} does not exist", 'tni-core' ) ); + } + + /* If the taxonomy doesn't exist, return an error */ + if( ( $old_taxonomy ) && !taxonomy_exists( $old_taxonomy ) ) { + echo "The taxonomy $old_taxonomy does not exist"; + return new WP_Error( 'taxonomy-invalid', __( "The taxonomy {$old_taxonomy} does not exist", 'tni-core' ) ); + } + + /* If query term was provided */ + if( $old_term ) { + $args['tax_query'] = array( + 'taxonomy' => $old_taxonomy, + 'field' => 'term_id', + 'terms' => (int) $old_term + ); + } + + /* If the ter doesn't exist, return an error */ + if( ( $new_term ) && !term_exists( $new_term, $new_taxonomy ) ) { + echo "The taxonomy term $new_term does not exist"; + return new WP_Error( 'term-invalid', __( "The taxonomy term {$new_term} does not exist", 'tni-core' ) ); + } + + /* If the taxonomy doesn't exist, return an error */ + if( ( $new_taxonomy ) && !taxonomy_exists( $new_taxonomy ) ) { + echo "The taxonomy $new_taxonomy does not exist"; + return new WP_Error( 'taxonomy-invalid', __( "The taxonomy {$new_taxonomy} does not exist", 'tni-core' ) ); + } + + $posts = get_posts( $args ); + + if( empty( $posts ) || is_wp_error( $posts ) ) { + echo "There are no posts that match the request"; + return new WP_Error( 'no-posts', __( 'There are no posts that match the $find request', 'tni-core' ) ); + } + + foreach( $posts as $post ) { + set_post_type( $post->ID, $new_post_type ); + + if( $new_term && $new_taxonomy ) { + wp_set_post_terms( $post->ID, $new_term, $new_taxonomy, false ); + } + } + + echo "DONE"; +} diff --git a/readme.txt b/readme.txt index 1d91491..990ee52 100644 --- a/readme.txt +++ b/readme.txt @@ -3,7 +3,7 @@ Contributors: misfist Tags: custom Requires at least: 4.7 Tested up to: 4.7.5 -Version: 1.2.4 +Version: 1.2.5 License: GPLv2 or later License URI: http://www.gnu.org/licenses/gpl-2.0.html @@ -22,6 +22,9 @@ This section describes how to install the plugin and get it working. == Changelog == += 1.2.5 May 22, 2017 = +* #20 Added utility function to switch post_type `tni_switch_post_type()` + = 1.2.4 May 18, 2017 = * #66 {https://github.com/thenewinquiry/tni-theme/issues/66} - Fix permalink for `future` posts diff --git a/tni-core-functionality.php b/tni-core-functionality.php index bd8b4d6..b5699b8 100644 --- a/tni-core-functionality.php +++ b/tni-core-functionality.php @@ -10,7 +10,7 @@ * Text Domain: tni-core * Domain Path: /languages * - * Version: 1.2.4 + * Version: 1.2.5 * * @package Tni_Core_Functionality */ @@ -52,7 +52,7 @@ * @return object Tni_Core */ function Tni_Core() { - $instance = Tni_Core::instance( __FILE__, '1.2.4' ); + $instance = Tni_Core::instance( __FILE__, '1.2.5' ); return $instance; }