首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >未登录或未从命令行登录时,wp_update_post ()不接受tax_input

未登录或未从命令行登录时,wp_update_post ()不接受tax_input
EN

Stack Overflow用户
提问于 2020-12-19 09:25:57
回答 1查看 56关注 0票数 0

为WordPress编写导入脚本,当登录到同一个WordPress站点时,可以从浏览器窗口正确执行导入。但是,从命令行(或从icognito窗口)执行脚本只能部分工作。它做了除了更新"cheatsheetcat“分类之外的所有事情。

我怀疑这与"public“旗帜有关,但我不完全理解。

分类设置如下:

代码语言:javascript
复制
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');

我认为导入脚本中失败的部分是:

代码语言:javascript
复制
$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更深层次的东西。

任何洞察力都将令人惊叹。

EN

回答 1

Stack Overflow用户

发布于 2021-07-28 17:25:14

在脚本开头设置用户wp_set_current_user为我解决了这个问题。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65365822

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档