Get all URL from Custom Post Type


Для плагина редиректов понадобилось вытянуть из базы все ссылки на кастомные посты + у меня в этих постах была сохранена старая ссылка от старого сайта. В итоге получился такой мини-плагин 🙂

п.с. Обратите внимание на эту строку $old_url = gallery_get_meta( 'gallery_old_pic_url' ); - тут я вытягиваю старую ссылку из кастомного поля поста

 

/**
 *
 * @wordpress-plugin
 * Plugin Name: Get all URL from Custom Post Type
 * Plugin URI:  http://coding.dp.ua
 * Description: Get all URL from Custom Post Type.
 * Version:     0.1.0
 * Author:      Roman NMSK
 * Author URI:  http://coding.dp.ua
 * License:     GPL-2.0+
 * License URI: http://www.gnu.org/licenses/gpl-2.0.txt
 */

function get_all_url(){
	global $post;
	
	$cat_term = 'gal_category';
	$custom_post_type = 'gallery';
	
	// Get all the categories
	$categories = get_terms( $cat_term );
	$url = '';

	// Loop through all the returned terms
	foreach ( $categories as $category ):

		// set up a new query for each category, pulling in related posts.
		$services = new WP_Query(
			array(
				'post_type' => $custom_post_type,
				'showposts' => -1,
				'tax_query' => array(
					array(
						'taxonomy'  => $cat_term,
						'terms'     => array( $category->slug ),
						'field'     => 'slug'
					)
				)
			)
		);

		while ($services->have_posts()) : $services->the_post();
			$old_url = gallery_get_meta( 'gallery_old_pic_url' );
			$new_url = get_permalink($post->ID);
			$string = '"'.$old_url.'","'.$new_url.'","0","url","301","url","0",""'."\n";
			$url .= $string;
		endwhile;

		$services = null;
		wp_reset_postdata();

	// end the loop
	endforeach;

	file_put_contents(plugin_dir_path(__FILE__).'urls.csv', $url);
}

add_action('init', 'get_all_url');

фыв

ПОЛЕЗНО  Исключить категории из результатов поиска WordPress