将自定义数据库表条目导入为带有类别的帖子

时间:2016-02-09 作者:Mike

我使用以下代码将自定义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 );

1 个回复
SO网友:Mike

已解决:只需添加trim 从自定义DB表中检索到的术语名称!!哦!

相关推荐

GET_THE_TERMS与wp_GET_POST_TERMS中的奇怪结果

我正在尝试制作一个面包屑函数,但有一个小问题。。。使用时:$categories = get_the_terms( $post->ID, \'product_cat\' ); 我得到了一个循环中使用的类别数组,等等。唯一的问题是它是按字母顺序排列的。(我希望它按层次顺序排列。)经过一番挖掘,我发现了一种替代方法,即使用wp\\u get\\u post\\u terms(),但我肯定遗漏了一些东西,因为当我使用此方法时:$categories = wp_get_post_terms( $p