1

I'm having an issue with custom post type "draft" post previews redirecting to the site home page, if I have pretty permalinks enabled.

For instance, I create a new "Project", save as draft, and click preview. The preview window opens to the site home page.

It works correctly if I set site permalinks to 'default'.

This is my code registering the post type. Per the note in the 'publicly_queryable' section in the register_post_type codex entry, I experimented with different settings for 'query_var' (true / false / 'projects') but none made any difference. I also tried editing the rewrite argument and/or removing it altogether. No go. Thanks for any help!

UPDATE: I have confirmed that this used to work, and broke with a recent update. I'm guessing it was the update to 4.2.2 mentioned in the 'publicly_queryable' section of the register_post_type codex page, but no one noticed until just now.

// Project
      $labels = array(
        'name'                => 'Projects',
        'singular_name'       => 'Project',
        'menu_name'           => 'Projects',
        'parent_item_colon'   => 'Parent Project:',
        'all_items'           => 'All Projects',
        'view_item'           => 'View Project',
        'add_new_item'        => 'Add New Project',
        'add_new'             => 'Add New',
        'edit_item'           => 'Edit Project',
        'update_item'         => 'Update Project',
        'search_items'        => 'Search Project',
        'not_found'           => 'Not found',
        'not_found_in_trash'  => 'Not found in Trash',
      );
       $rewrite = array(
        'slug'                       => 'projects',
        'with_front'                 => true,
        'hierarchical'               => true,
      );
      $args = array(
        'label'               => 'Project',
        'description'         => 'Individual Projects',
        'labels'              => $labels,
        'supports'            => array( 'title', 'editor', 'revisions', 'author'),
        'taxonomies'          => array( 'project_categories', 'project_sponsors' ),
        'hierarchical'        => false,
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'menu_position'       => 20,
        'menu_icon'           => 'dashicons-clipboard',
        'can_export'          => true,
        'has_archive'         => true,
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
        'capability_type'     => 'page',
        'rewrite'                    => $rewrite,
       'query_var'              => 'projects',

      );
      register_post_type( 'projects', $args );
  • Have you saved your permalinks since you created your post type? Can you view it live on the front-end without problems? – Howdy_McGee Sep 29 '15 at 18:34
  • Yes, I've been resaving permalinks between each change to the CPT. The regular-view of the CPT has always worked fine, it's only the Draft-status previews not working. Once published, the preview works. But my authors want to preview before publishing. :) – Michelle Sep 29 '15 at 18:38
  • I used your code as is. It worked fine in WordPress 4.3.1. Could you please confirm your WP version? Also if you are registering this post type in a plugin you may like to consider 'Flushing Rewrite On Activation' instead of manual permalink flushing. – Prasad Nevase Sep 30 '15 at 5:51
1

The code you provided works as expected on a fresh install of WordPress (4.3.1 in this case). The ideas below shouldn't be considered an answer, so much as a checklist to aid in debugging the issue by narrowing the scope of the problem:

  1. You'll need to be logged in as a user with permission to view drafs to avoid a 404 or redirect.

  2. Try disabling your plugins one by one to see if perhaps one of the plugins has established a conflicting rewrite rule.

  3. Check your web server configuration (such as .htaccess on Apache) to see if there are any redirect rules in place there.

  4. Finally, consider temporarily switching to another theme and adding your custom post type there to see if you get the same results. If a new theme solves the issue with your code, perhaps the conflicts all exist within your current theme.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.