我使用以下代码将自定义DB表中的所有条目作为POST导入。该函数从init挂钩激发。
创建了所有分类法,并导入了数据库中的所有条目,但我发现数据库中有一半条目没有设置它们的术语。另一半是,我一辈子都不知道为什么。
我打开了完全调试模式,还使用了is\\u wp\\u error,但没有输出任何提示问题的内容。
<?php
/**
* Set the playlist entry category.
*
* @since 1.3
* @param int $event_id The event ID.
* @param int $term_id The category term ID.
* @return bool True on success, otherwise false.
*/
function mdjm_set_playlist_entry_category( $event_id, $term_id ) {
$set_entry_type = wp_set_post_terms( $event_id, $term_id, \'playlist-category\' );
if ( is_wp_error( $set_entry_type ) ) {
return false;
}
else {
return true;
}
} // mdjm_set_playlist_entry_category
/**
* Create terms for each of the playlist categories.
*
* @since 1.3
* @param
* @return void
*/
function mdjm_create_playlist_terms() {
global $wpdb;
$cats = mdjm_get_option( \'playlist_cats\' );
$terms = explode( "\\r\\n", $cats );
if ( ! empty( $terms ) ) {
foreach( $terms as $term ) {
$new_term = wp_insert_term( $term, \'playlist-category\' );
if( is_wp_error( $new_term ) ) {
error_log( $new_term->get_error_message() );
}
}
}
wp_insert_term( __( \'Guest\', \'mobile-dj-manager\' ), \'playlist-category\' );
} // mdjm_create_playlist_terms
/**
* Create terms for each of the playlist categories.
*
* @since 1.3
* @param
* @return void
*/
function mdjm_import_playlist_entries() {
global $wpdb;
if( get_option( \'mdjm_playlist_import\' ) ) {
return;
}
// Create the terms
mdjm_create_playlist_terms();
$query = "SELECT * FROM
" . $wpdb->prefix . "mdjm_playlists";
$entries = $wpdb->get_results( $query );
if( $entries ) {
add_option( \'mdjm_playlist_import\', false );
foreach( $entries as $entry ) {
$meta = array(
\'song\' => isset( $entry->song ) ? $entry->song : \'\',
\'artist\' => isset( $entry->artist ) ? $entry->artist : \'\',
\'added_by\' => isset( $entry->added_by ) ? $entry->added_by : get_current_user_id(),
\'djnotes\' => isset( $entry->info ) ? $entry->info : \'\',
\'added_date\' => isset( $entry->date_added ) ? $entry->date_added : \'\',
\'to_mdjm\' => isset( $entry->date_to_mdjm ) ? date( \'Y-m-d H:i:s\', strtotime( $entry->date_to_mdjm ) ) : \'\',
\'uploaded\' => isset( $entry->upload_procedure ) ? $entry->upload_procedure : \'\',
);
$term = isset( $entry->play_when ) ? $entry->play_when : \'General\';
$event_id = isset( $entry->event_id ) ? $entry->event_id : \'\';
if( empty( $term ) || $term == \'Guest Added\' ) {
$term = \'Guest\';
}
$title = sprintf( __( \'Event ID: %s %s %s\', \'mobile-dj-manager\' ),
mdjm_get_option( \'event_prefix\', \'\' ) . $event_id,
$meta[\'song\'],
$meta[\'artist\'] );
$category = get_term_by( \'name\', $term, \'playlist-category\' );
$entry_id = wp_insert_post(
array(
\'post_type\' => \'mdjm-playlist\',
\'post_title\' => $title,
\'post_author\' => 1,
\'post_status\' => \'publish\',
\'post_parent\' => $event_id,
\'post_date\' => isset( $entry->date_added )? date( \'Y-m-d H:i:s\', strtotime( $entry->date_added ) ) : date( \'Y-m-d H:i:s\' ),
\'post_category\' => !empty( $category ) ? array( $category->term_id ) : \'\'
)
);
if( ! empty( $category ) ) {
mdjm_set_playlist_entry_category( $entry_id, $category->term_id );
}
foreach( $meta as $key => $value ) {
update_post_meta( $entry_id, \'_mdjm_playlist_entry_\' . $key, $value );
}
}
update_option( \'mdjm_playlist_import\', true );
}
} // mdjm_import_playlist_entries
add_action( \'init\', \'mdjm_import_playlist_entries\', 15 );