jon-b
9/13/2013 - 11:26 PM

Three functions for manipulating existing WordPress data

Three functions for manipulating existing WordPress data

<?php
/**
 * echo_r function.
 * 
 * Jon's favorite little debug function
 *
 * @access public
 * @param mixed $array
 * @return void
 */
if (!function_exists('echo_r')) {
  function echo_r($array) {
		echo '<pre>' , print_r($array) , '</pre>';
	}
}

//add_action('genesis_meta','zdhix_move_faqs_to_parents');
/**
 * zdhix_move_faqs_to_parents function.
 * 
 * Ths function moves FAQ pages imported via WP All Import under thier state named Parent
 *
 * @access public
 * @return void
 */
function zdhix_move_faqs_to_parents() {
  if (!is_admin()) {

		$args = array(
			'numberposts' => -1, // Adjust quantity
			'post_type' => 'page',  // Setup post type
			'meta_key' => '_wp_page_template',
			'meta_value' => 'faq-template.php',
		);
		$mvposts = get_pages( $args );
		//echo_r($mvposts);
		if (is_array($mvposts)) {
			foreach ($mvposts as $mvpost) {
				$state = (get_post_meta($mvpost->ID , 'faq_state', true));
				echo '$state ' . $state . '<br/>';

				$statepost = get_page_by_title($state);
				echo '$statepost->ID' , $statepost->ID , '<br/>';

				$mvpost->post_parent = $statepost->ID;  // Set new parent 
				echo '$mvpost->post_parent' . $mvpost->post_parent . '<br/>';;

				wp_update_post( $mvpost ); // Update posts

				echo "Moved Post: " . $mvpost->post_title . "\r\n<br/>";
			}
			wp_die();
		}
	}
}


//add_action('genesis_meta','zdhix_move_faq_meta_to_taxonomy');
/**
 * zdhix_move_faqs_to_parents function.
 * 
 * Ths function moves updates FAQ taxonomies based on meta data
 *
 * @access public
 * @return void
 */
function zdhix_move_faq_meta_to_taxonomy() {
	if (!is_admin()) {

		$args = array(
			'numberposts' => -1, // Adjust quantity
			'post_type' => 'page',  // Setup post type
			'hierarchical' => 0,
//			'include' => array('3093'), // Single post for debug/testing, "First FAQ in ALL" #TODO verify has parent post meta
			'meta_key' => '_wp_page_template',
			'meta_value' => 'faq-template.php',
		);
		$myposts = get_pages( $args );
		//echo_r($myposts);
		if (is_array($myposts)) {
			foreach ($myposts as $mypost) {

				$cat = get_post_meta($mypost->ID , 'faq_category_meta', true);
				$cat_term = get_term_by('name', $cat, 'faq-category' );
				$cat_id = $cat_term->term_id;
//				echo '$cat ' . $cat . ' ' . $cat_id . '<br/>';
				wp_set_post_terms( $mypost->ID, array(71), 'page-category'); // Update page cat to is_faq (71)
				echo "Updated page-category to 71 for " . $mypost->post_title . "<br/>";

				$top = get_post_meta($mypost->ID , 'faq_top_meta', true);
				if ('Yes' == $top ) {
					$top_term = get_term_by('name', 'is_tops', 'faq-category');

					$top_id = $top_term->term_id;
					echo '<br/>$top ' . $top . ' ' . $top_id . '<br/>';
					wp_set_post_terms( $mypost->ID, array($cat_id, $top_id ), 'faq-category'); // Update faq cat
					echo "Updated faq-category to is_top" . $mypost->post_title . "<br/>";
				}
			}
			wp_die();
		}
	}
}


//add_action('genesis_meta','zdhix_fix_state_tax');
/**
 * zdhix_move_faqs_to_parents function.
 * 
 * Ths function moves updates FAQ taxonomies based on meta data
 *
 * @access public
 * @return void
 */
function zdhix_fix_state_tax() {
	if (!is_admin()) {

		$args = array(
			'numberposts' => 0, // Adjust quantity
			'post_type' => 'page',  // Setup post type
			'hierarchical' => 0,
//			'include' => array('4243'), // Single post for debug/testing, "All"
			'meta_key' => '_wp_page_template',
			'meta_value' => 'state-template.php',
		);
		$myposts = get_pages( $args );
		//echo_r($myposts);
		if (is_array($myposts)) {
			foreach ($myposts as $mypost) {

				wp_set_post_terms( $mypost->ID, '70', 'page-category'); // Update page cat to is_state (70)
				echo "Updated tax for " . $mypost->post_title . "\r\n<br/>";
			}
			wp_die();
		}
	}
}



//add_action('genesis_meta','zdhix_delete_faqs');
/**
 * zdhix_move_faqs_to_parents function.
 * 
 * Ths function moves FAQ pages imported via WP All Import under thier state named Parent
 *
 * @access public
 * @return void
 */
function zdhix_delete_faqs() {
echo 'starting delete posts<br/>';
	if (!is_admin()) {
		$args = array(
			'posts_per_page' => -1, // Adjust quantity
			'post_type' => 'page',  // Setup post type
			'meta_key' => '_wp_page_template',
			'meta_value' => 'faq-template.php',
		);
		$delposts = new 	WP_Query( $args );

		if($delposts->have_posts()) : while($delposts->have_posts()) : $delposts->the_post();
//			echo_r($delposts);
				       echo 'Deleting Post: '.$delposts->post->post_title.'<br/>';
				       wp_delete_post( $delposts->post->ID, true); // Delete posts
		
		endwhile;
		
		endif;	

		wp_die();
	}
}