我的问题是,第二个类别(应该根据记录的内容进行更改)被永久设置为所有正在处理的第一个职位。有没有办法修改下面的代码,以便更好地指定它应该返回在该特定记录中找到的类别?使用“save\\u post”挂钩同时发布多篇文章是否可能有问题?
在最近的一次更新中更改了前面提到的人)和另一个人,这取决于“categ”列的内容。相同的类别名称已作为类别插入网站。
是否有一种方法可以调用一个函数,根据特定帖子/记录的“categ”列的内容为每个帖子设置第二个类别?Aka,而不是所有人都有相同的;“数据科学”;(如第4幅图所示),我想分别设置它们,如下所示。
这将反映为帖子中列出的类别
Edit 2: Content in Publication Plugin
下面是我用来创建CPT的整个插件,从表中获取详细信息并插入它们: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( \'Student 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 submitted by students from the Faculty of ICT\', \'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 = [];
$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 )
{
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()
{
$available_in_cpt = verify_existing_abstract_in_cpt();
$database_results = query_abstract_post_table( $available_in_cpt );
if (NULL === $database_results || empty( $database_results ) || 0 === $database_results)
{
return;
}
//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\',
);
wp_insert_post( $abstract_details );
}
}
然后由content.php
文件并直接添加到其中,例如,esc_html(get_post_meta(get_the_ID(), \'firstabstractimage\', true))
(我知道将content.php更改为CPT内容并不理想,很快就会修改,以从archive-abstracts.php
文件)