Skip to content

Commit

Permalink
Added utility function
Browse files Browse the repository at this point in the history
#20 Added utility function to switch post_type `tni_switch_post_type()`
  • Loading branch information
misfist committed May 22, 2017
1 parent 5533b24 commit 1c81aec
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 3 deletions.
85 changes: 85 additions & 0 deletions includes/utilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
5 changes: 4 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down
4 changes: 2 additions & 2 deletions tni-core-functionality.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* Text Domain: tni-core
* Domain Path: /languages
*
* Version: 1.2.4
* Version: 1.2.5
*
* @package Tni_Core_Functionality
*/
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 1c81aec

Please sign in to comment.