用 wp_insert_posts 插入的帖子未显示在管理员中

Posts inserted with wp_insert_posts aren't showing up in admin

我在使用 wp_insert_posts.

插入自定义 post 时遇到了一个特殊问题

情况:

到目前为止我尝试了什么

Post插入代码:

$post_id = wp_insert_post([
        "post_type" => "order",
        "post_content" => "",
        "post_title" => $order->Name() . '<' . $order->Email() . '>',
        "post_status" => "publish",
        "post_author" => 1
    ]);

    update_post_meta($post_id, 'order_details', '{"some":"json here"}');

post 被插入到 table 中,管理中的列表页面显示它在总计数中,但 post 没有显示在列表本身中.

Post输入注册码

if ( ! function_exists('ls_so_order') ) {

// Register Custom Post Type
function ls_so_order() {

    $labels = array(
        'name'                  => _x( 'Orders', 'Post Type General Name', 'ls-simple-order' ),
        'singular_name'         => _x( 'Order', 'Post Type Singular Name', 'ls-simple-order' ),
        'menu_name'             => __( 'Orders', 'ls-simple-order' ),
        'name_admin_bar'        => __( 'ProductOrder', 'ls-simple-order' ),
        'archives'              => __( 'Order Archives', 'ls-simple-order' ),
        'attributes'            => __( 'Order Attributes', 'ls-simple-order' ),
        'parent_item_colon'     => __( 'Parent Order:', 'ls-simple-order' ),
        'all_items'             => __( 'All Orders', 'ls-simple-order' ),
        'add_new_item'          => __( 'Add New Order', 'ls-simple-order' ),
        'add_new'               => __( 'Add New', 'ls-simple-order' ),
        'new_item'              => __( 'New Order', 'ls-simple-order' ),
        'edit_item'             => __( 'Edit Order', 'ls-simple-order' ),
        'update_item'           => __( 'Update Order', 'ls-simple-order' ),
        'view_item'             => __( 'View Order', 'ls-simple-order' ),
        'view_items'            => __( 'View Orders', 'ls-simple-order' ),
        'search_items'          => __( 'Search Order', 'ls-simple-order' ),
        'not_found'             => __( 'Not found', 'ls-simple-order' ),
        'not_found_in_trash'    => __( 'Not found in Trash', 'ls-simple-order' ),
        'featured_image'        => __( 'Featured Image', 'ls-simple-order' ),
        'set_featured_image'    => __( 'Set featured image', 'ls-simple-order' ),
        'remove_featured_image' => __( 'Remove featured image', 'ls-simple-order' ),
        'use_featured_image'    => __( 'Use as featured image', 'ls-simple-order' ),
        'insert_into_item'      => __( 'Insert into order', 'ls-simple-order' ),
        'uploaded_to_this_item' => __( 'Uploaded to this order', 'ls-simple-order' ),
        'items_list'            => __( 'Orders list', 'ls-simple-order' ),
        'items_list_navigation' => __( 'Orders list navigation', 'ls-simple-order' ),
        'filter_items_list'     => __( 'Filter orders list', 'ls-simple-order' ),
    );
    $args = array(
        'label'                 => __( 'Order', 'ls-simple-order' ),
        'description'           => __( 'Simple Order Orders', 'ls-simple-order' ),
        'labels'                => $labels,
        'supports'              => array( 'title', 'thumbnail', 'custom-fields' ),
        'hierarchical'          => false,
        'public'                => true,
        'show_ui'               => true,
        'show_in_menu'          => true,
        'menu_position'         => 20,
        'menu_icon'             => 'dashicons-cart',
        'show_in_admin_bar'     => true,
        'show_in_nav_menus'     => false,
        'can_export'            => true,
        'has_archive'           => false,
        'exclude_from_search'   => true,
        'publicly_queryable'    => false,
        'capability_type'       => 'page',
    );
    register_post_type( 'order', $args );

}
add_action( 'init', 'ls_so_order', 0 );

}

你可以尝试这样做吗?

    $post_data = array(
        'post_title' => 'Test regex',
        'post_content' =>  $order->Name() . '<' . $order->Email() . '>',
        'post_type' => 'post',
        'post_status' => 'publish',
        'post_author' => 1, 
);

    $post_id = wp_insert_post($post_data);
    update_post_meta($post_id, 'order_details', '{"some":"json here"}');

在我能找到的每个论坛上来回走动,并修改@miguelcalderons 的答案后,我找到了罪魁祸首,这是一个非常简单但隐藏的东西。

WordPress Reserved Terms

WordPress 有一长串保留字词,order 就是其中之一。

用不同的字符串替换自定义 post 类型 slug 后,它立即开始工作。