CPT not showing in export

User
custom post type option not showing to export in tools export wordpress
Assistant
If your custom post type (CPT) isn't showing up in the "Tools > Export" section in WordPress, it might be due to how the custom post type was registered. Specifically, the issue usually comes from the `show_in_rest` or `can_export` arguments being incorrectly set (or omitted) when registering the CPT.

Here's what you can check or update in your code:

### 1. Check the `register_post_type` Arguments
Make sure that your custom post type is registered with `can_export` set to `true`. If this is missing or set to `false`, WordPress won't include it in the export options.

Here's an example of how to register a custom post type correctly:

function register_custom_post_type() {
    register_post_type('my_custom_post_type', [
        'label' => 'My Custom Post Type',
        'public' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'show_in_rest' => true, // Ensure it's available in REST API (optional but recommended)
        'has_archive' => true,
        'supports' => ['title', 'editor', 'thumbnail'],
        'can_export' => true, // This is crucial
    ]);
}
add_action('init', 'register_custom_post_type');

### 2. Ensure `can_export` is Set to `true`
If your CPT registration code is missing the `can_export` argument, add it as shown above. The default value is `true`, but explicitly including it can help avoid issues.

### 3. Clear Cache
If you've updated your CPT registration, clear your browser cache and any WordPress caching plugins, then check again in "Tools > Export."

### 4. Flush Rewrite Rules
Sometimes, WordPress needs to refresh its permalink structure to recognize changes. You can flush the rewrite rules by going to **Settings > Permalinks** and simply clicking "Save Changes." Alternatively, you can do it programmatically:

flush_rewrite_rules();

Add this temporarily to your `functions.php` file, then reload your site once and remove it afterward.

### 5. Check for Conflicts
If it's still not showing up, deactivate other plugins and switch to a default theme (like Twenty Twenty-Three) to check if there's a conflict.

### 6. Debug by Checking Post Types Available for Export
To see which post types are available for export, add this snippet to your theme's `functions.php` and check the output:

add_action('admin_init', function () {
    $post_types = get_post_types(['can_export' => true], 'names');
    echo '<pre>';
    print_r($post_types);
    echo '</pre>';
});

This will output a list of all post types that are available for export. Make sure your custom post type is listed.

These steps should help you get your custom post type to show up in the WordPress export tool.

User
show_in_rest this option to previously registered post types
Assistant
If you have already registered custom post types and want to add the `show_in_rest` option (to make them compatible with the WordPress REST API and block editor), you can update the existing registration code without having to delete and recreate the post types.

### How to Add `show_in_rest` to Previously Registered Post Types

Here's how you can modify your existing custom post type registration to include `show_in_rest => true`:

1. **Locate the Custom Post Type Registration Code**
Find the place in your theme's `functions.php` file (or wherever you registered your custom post types) and update it as follows:

function modify_custom_post_type() {
       // Update the registration of the existing custom post type
       $args = [
           'label' => 'My Custom Post Type',
           'public' => true,
           'show_ui' => true,
           'show_in_menu' => true,
           'has_archive' => true,
           'supports' => ['title', 'editor', 'thumbnail'],
           'can_export' => true,
           'show_in_rest' => true, // Add this line
       ];

       register_post_type('my_custom_post_type', $args);
   }
   add_action('init', 'modify_custom_post_type');

2. **Save Changes and Refresh**
After updating the code, save the file and refresh your WordPress admin panel.

3. **Flush Rewrite Rules (Optional but Recommended)**
To ensure WordPress recognizes the updated post type settings, you can flush the rewrite rules:

– Go to **Settings > Permalinks** and click "Save Changes" without altering anything.
– Alternatively, add this code temporarily in your `functions.php` file and then remove it:

add_action('init', function () {
         flush_rewrite_rules();
     });

### Programmatically Update `show_in_rest` for All Existing Custom Post Types

If you have multiple custom post types and want to enable `show_in_rest` for all of them without manually updating each registration, you can use this snippet:

function enable_show_in_rest_for_existing_cpts() {
    $post_types = get_post_types(['public' => true], 'objects');

    foreach ($post_types as $post_type) {
        if ($post_type->_builtin === false) { // Only apply to non-default post types
            $args = (array) $post_type;
            $args['show_in_rest'] = true;
            register_post_type($post_type->name, $args);
        }
    }
}
add_action('init', 'enable_show_in_rest_for_existing_cpts', 20);

### Notes
– This solution ensures that all previously registered custom post types are updated with `show_in_rest => true`.
– The above code will only apply to custom (non-builtin) post types.
– Always back up your site before making changes to core files like `functions.php`.

This approach will allow you to enable REST API support for custom post types without needing to register them from scratch.

Leave a Comment

Your email address will not be published. Required fields are marked *