为WordPress编写导入脚本,当登录到同一个WordPress站点时,可以从浏览器窗口正确执行导入。但是,从命令行(或从icognito窗口)执行脚本只能部分工作。它做了除了更新"cheatsheetcat“分类之外的所有事情。
我怀疑这与"public“旗帜有关,但我不完全理解。
分类设置如下:
function pios_add_cheatsheet_taxonomy()
{
$labels = array(
'name' => 'Cheat Sheet Categories',
'singular_name' => 'Cheat Sheet Category',
'search_items' => 'Search Cheat Sheet Category',
'all_items' => 'All Cheat Sheet Categories',
'parent_item' => 'Parent Cheat Sheet Category',
'parent_item_colon' => 'Parent Cheat Sheet Category:',
'edit_item' => 'Edit Cheat Sheet Category',
'update_item' => 'Update Cheat Sheet Category',
'add_new_item' => 'Add New Cheat Sheet Category',
'new_item_name' => 'New Cheat Sheet Category',
'menu_name' => 'Cheat Sheet Category',
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'query_var' => true,
'rewrite' => true,
'public' => true,
'show_admin_column' => true,
);
register_taxonomy('cheatsheetcat', 'attachment', $args);
//Add the parent categories
$ar_parent_categories = array("Item category", "Industry", "Family", "BeerWine");
foreach ($ar_parent_categories as $category) {
$item_term = term_exists($category, 'cheatsheetcat', 0);
// Create state if it doesn't exist
if (!$item_term) {
$item_term = wp_insert_term($category, 'cheatsheetcat', array('parent' => 0));
}
}
}
add_action('init', 'pios_add_cheatsheet_taxonomy');我认为导入脚本中失败的部分是:
$custom_tax = array(
'cheatsheetcat' => $arTaxCheetSheet
);
//Update the attachment / post
$attachment = [
'ID' => $id,
'post_title' => $filename,
'post_content' => $content,
'post_excerpt' => $excerpt,
'tax_input' => $custom_tax,
];
$post_id = wp_update_post ($attachment, true);
if (is_wp_error($post_id)) {
$errors = $post_id->get_error_messages();
foreach ($errors as $error) {
echo $error;
}
}此时的$arTaxCheetSheet是正确的,已经将其打印到屏幕上。但我遗漏了WordPress更深层次的东西。
任何洞察力都将令人惊叹。
发布于 2021-07-28 17:25:14
在脚本开头设置用户wp_set_current_user为我解决了这个问题。
https://stackoverflow.com/questions/65365822
复制相似问题