我有一个phpMyAdmin数据库,其中有一个表,当从数据库输入其内容时,会自动发布相关信息。
记录中的一些详细信息可能为NULL,在测试期间,我遇到了一个问题,即如果任何记录中的任何内容为NULL,那么从wp admin重新加载时,所有记录都会重复。数据库中的记录数保持不变。
我很确定在这种情况下有一种处理null的方法,但我对WordPress和PHP总体上非常缺乏经验,也找不到相关的主题。
以下是相关代码和屏幕截图,以更好地解释上下文:
Publication-CPT.php (插件)
if(!function_exists(\'add_action\'))
{
echo \'ERROR: ABSPATH UNDEFINED. Access to this file is not allowed.\';
exit;
}
function create_abstract_cpt() {
$labels = array(
\'name\' => _x( \'Abstracts\', \'Post Type General Name\', \'textdomain\' ),
\'singular_name\' => _x( \'Abstract\', \'Post Type Singular Name\', \'textdomain\' ),
\'menu_name\' => _x( \'Abstracts\', \'Admin Menu text\', \'textdomain\' ),
\'name_admin_bar\' => _x( \'Abstract\', \'Add New on Toolbar\', \'textdomain\' ),
\'archives\' => __( \'Abstract Archives\', \'textdomain\' ),
\'attributes\' => __( \'Abstract Attributes\', \'textdomain\' ),
\'parent_item_colon\' => __( \'Parent Abstract:\', \'textdomain\' ),
\'all_items\' => __( \'All Abstracts\', \'textdomain\' ),
\'add_new_item\' => __( \'Add New Abstract\', \'textdomain\' ),
\'add_new\' => __( \'Add New\', \'textdomain\' ),
\'new_item\' => __( \'New Abstract\', \'textdomain\' ),
\'edit_item\' => __( \'Edit Abstract\', \'textdomain\' ),
\'update_item\' => __( \'Update Abstract\', \'textdomain\' ),
\'view_item\' => __( \'View Abstract\', \'textdomain\' ),
\'view_items\' => __( \'View Abstracts\', \'textdomain\' ),
\'search_items\' => __( \'Search Abstract\', \'textdomain\' ),
\'not_found\' => __( \'Not found\', \'textdomain\' ),
\'not_found_in_trash\' => __( \'Not found in Trash\', \'textdomain\' ),
\'featured_image\' => __( \'Featured Image\', \'textdomain\' ),
\'set_featured_image\' => __( \'Set featured image\', \'textdomain\' ),
\'remove_featured_image\' => __( \'Remove featured image\', \'textdomain\' ),
\'use_featured_image\' => __( \'Use as featured image\', \'textdomain\' ),
\'insert_into_item\' => __( \'Insert into Abstract\', \'textdomain\' ),
\'uploaded_to_this_item\' => __( \'Uploaded to this Abstract\', \'textdomain\' ),
\'items_list\' => __( \'Abstracts list\', \'textdomain\' ),
\'items_list_navigation\' => __( \'Abstracts list navigation\', \'textdomain\' ),
\'filter_items_list\' => __( \'Filter Abstracts list\', \'textdomain\' ),
);
$args = array(
\'label\' => __( \'Abstract\', \'textdomain\' ),
\'description\' => __( \'List of all the abstracts\', \'textdomain\' ),
\'labels\' => $labels,
\'menu_icon\' => \'dashicons-text\',
\'supports\' => array(\'title\', \'editor\', \'excerpt\', \'thumbnail\', \'author\', \'post-formats\', \'custom-fields\'),
\'public\' => true,
\'show_ui\' => true,
\'show_in_menu\' => true,
\'menu_position\' => 5,
\'show_in_admin_bar\' => true,
\'show_in_nav_menus\' => true,
\'can_export\' => true,
\'has_archive\' => true,
\'hierarchical\' => false,
\'exclude_from_search\' => false,
\'show_in_rest\' => true,
\'publicly_queryable\' => true,
\'capability_type\' => \'post\',
\'taxonomies\' => array( \'category\', \'post_tag\' ),
);
flush_rewrite_rules();
register_post_type( \'abstract\', $args );
}
add_action( \'init\', \'create_abstract_cpt\', 0 );
add_action( \'wp\', \'insert_into_cpt\');
function verify_existing_abstract_in_cpt()
{
$id_arrays_in_cpt = [];
// Query all autos
$args = array(
\'post_type\' => \'abstract\',
\'posts_per_page\' => -1,
);
$loop = new WP_Query($args);
while ( $loop->have_posts() ) {
$loop->the_post();
$id_arrays_in_cpt[] = get_post_meta( get_the_ID(), \'abstract_id\', true);
}
return $id_arrays_in_cpt;
}
function query_abstract_post_table( $available_in_cpt )
{
// Query Database
global $wpdb;
$table_name = $wpdb->prefix . \'abstract_posts\';
if ( NULL === $available_in_cpt || empty($available_in_cpt) || 0 === $available_in_cpt) {
$sql = "SELECT * FROM $table_name";
} else {
$ids = implode( ",", $available_in_cpt);
$sql = "SELECT * FROM $table_name WHERE abstract_id NOT IN (\'$ids\')";
}
$results = $wpdb->get_results( $sql );
return $results;
}
function insert_into_cpt()
{
//If the queried results from do not match the existing abstracts
$available_in_cpt = verify_existing_abstract_in_cpt();
$database_results = query_abstract_post_table( $available_in_cpt );
//Insert into CPT
foreach($database_results as $result)
{
// Create post object
$abstract_details = array(
\'post_title\' => $result->title,
\'meta_input\' => array(
\'abstract_id\' => $result->abstract_id,
\'title\' => $result->title,
\'author\' => $result->author,
\'supervisor\' => $result->supervisor,
\'cosupervisor\' => $result->cosupervisor,
\'course\' => $result->course,
\'categ\' => $result->categ,
\'writeup\' => $result->writeup,
\'firstabstractimage\' => $result->firstabstractimage,
\'firstabstractimagecaption\' => $result->firstabstractimagecaption,
\'secondabstractimage\' => $result->secondabstractimage,
\'secondabstractimagecaption\' => $result->secondabstractimagecaption,
\'ref\' => $result->ref,
),
\'post_type\' => \'abstract\',
\'post_status\' => \'publish\',
);
//Insert into wp_insert_post
wp_insert_post( $abstract_details );
}
}
wp_abstract_posts (表)-任何记录中包含NULL都会导致此问题
Abstracts CPT posts (以任何方式刷新wp admin时复制所有帖子)
任何帮助都将不胜感激。