Search code examples
ajaxwordpresscustom-post-typemultisitewpml

Posts inserted with wp_insert_posts aren't showing up in admin


I have a particular issue with inserting custom posts with wp_insert_posts.

Circumstances:

  • WPML
  • Multisite enabled
  • Custom post type with name order
  • Post creation triggered through Ajax from frontend
  • The post is inserted in the original language, no translations

What have I tried so far

  • Checked the post type register code to see if it contains any incompatible settings, tried all combinations -> noting. This is what is the closest thing to my problem: wp_insert_post entries not showing in admin content list

    • The resolution there is not applicable to me, as I execute it through Ajax, and WP is already initialized at the Ajax hook
  • Went through WPML forums with all their posts related to similar issues, tried the steps provided there (although most of them talk about adding translations)

  • Tried various other solutions

Post insert code:

$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"}');

The post gets inserted in the table, and the listing page in admin shows it in the total counts, but the post doesn't show up in the list itself.

Empty list

Post type register code

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 );

}

Solution

  • After going back and forth in every forum I could find, and tinkering with @miguelcalderons answer, I found the culprit, and it is a very simple, although quiet hidden thing.

    WordPress Reserved Terms

    WordPress has a quiet long list of reserved terms, and order being one of them.

    After replacing the custom post type slug with a different string, it started working immediately.