0

I have created a custom post type within a custom plugin.

Here is my post type definition:

function hrd_record_custom_post_type() {

    $args = array(
        'description'         => __( 'hrdrecord', 'hrd_record' ), 
        'public'              => true, 
        'publicly_queryable'  => true, 
        'exclude_from_search' => false, 
        'show_in_nav_menus'   => true, 
        'show_ui'             => true, 
        'show_in_menu'        => true, 
        'show_in_admin_bar'   => true, 
        'menu_position'       => null, 
        'menu_icon'           => null, 
        'can_export'          => true, 
        'delete_with_user'    => false, 
        'hierarchical'        => false, 
        'has_archive'         => true, 
        'query_var'           => true, 
        'capability_type'     => 'hrdrecord', 
        'map_meta_cap'        => true, 
        'capabilities' => array(

            'edit_post'              => 'edit_hrdrecord',
            'read_post'              => 'read_hrdrecord',
            'delete_post'            => 'delete_hrdrecord',
            'create_posts'           => 'create_hrdrecords',
            'edit_posts'             => 'edit_hrdrecords',
            'edit_others_posts'      => 'edit_others_hrdrecords',
            'publish_posts'          => 'publish_hrdrecords',
            'read_private_posts'     => 'read_private_hrdrecords',
            'read'                   => 'read',
            'delete_posts'           => 'delete_hrdrecords',
            'delete_private_posts'   => 'delete_private_hrdrecords',
            'delete_published_posts' => 'delete_published_hrdrecords',
            'delete_others_posts'    => 'delete_others_hrdrecords',
            'edit_private_posts'     => 'edit_private_hrdrecords',
            'edit_published_posts'   => 'edit_published_hrdrecords'
        ),

        'rewrite' => array(
            'slug'       => 'hrdrecord', 
            'with_front' => false, 
            'pages'      => true, 
            'feeds'      => true, 
            'ep_mask'    => EP_PERMALINK, 
        ),


        'taxonomies' => array( 'location','sex','type-of-work','type-of-killing','previous-threat', 'gender-component', 'status-of-investigation','sector','sector-detail'),
        'supports' => array(
            'author',
            'thumbnail',
        ),

        'labels' => array(
            'name'               => __( 'HRD Record',                   'hrd_record' ),
            'singular_name'      => __( 'HRD Record',                    'hrd_record' ),
            'menu_name'          => __( 'HRD Record',                   'hrd_record' ),
            'name_admin_bar'     => __( 'HRD Record',                   'hrd_record' ),
            'add_new'            => __( 'Add New',                    'hrd_record' ),
            'add_new_item'       => __( 'Add New HRD Record',            'hrd_record' ),
            'edit_item'          => __( 'Edit HRD Record',               'hrd_record' ),
            'new_item'           => __( 'New HRD Record',                'hrd_record' ),
            'view_item'          => __( 'View HRD Record',               'hrd_record' ),
            'search_items'       => __( 'Search HRD Records',            'hrd_record' ),
            'not_found'          => __( 'No HRD Record found',          'hrd_record' ),
            'not_found_in_trash' => __( 'No HRD Record in trash',       'hrd_record' ),
            'all_items'          => __( 'All HRD Records',               'hrd_record' ),

            'archive_title'      => __( 'hrdrecord',                   'hrd_record' ),
        )
    );

    register_post_type(
        'hrdrecord', 
        $args      
    );


}
add_action( 'init', 'hrd_record_custom_post_type' );

The problem I am having is that when I have one of these post types in Draft and I click the Preview button (either from the All HRD Records listing or from the individual HRD Record edit screen) I am brought to a 404 Not Found page. But if the post is published, I have no problems Previewing Changes before updating or otherwise viewing the post type.

One thing that is also odd is that when I try to Preview from the listing, the URL has query parameters in the link of ?post_type=hrdrecord&p=999&preview=true But once this appears in the browser the &preview=true query variable disappears and even if I try to manually retype it, once I hit return, it is removed from the URL (only the ?post_type=hrdrecord&p=999 remain).

When I try the Preview button on the individual screen this string of variables is rewritten as ?post_type=hrdrecord&p=999&preview_id=999&preview_nonce=some set of numbers and letters but it still goes to a 404 not found page.

I have tried a number of things that I've found here and in other forums about similar problems but nothing is sorting it.

  1. I have tried changing themes
  2. I have tried removing parameters in the post type definition that are being set to their default values
  3. I have reset the Permalinks a number of times (also my plugin has a flush_rewrite_rules(); in both the activation and deactivation hooks and I have deactivated and re-activated the plugin a couple of times)
  4. I have changed the 'with_front' parameter in the 'rewrite' to true
  5. I have disabled all other plugins

So I am now at a loss. I am assuming it is related to rewrites and some parameter in the custom post type definition but I don't know which one or how to get Wordpress to associate the preview query variables with the post type.

Any suggestions would be most appreciated.

0

I had a colleague take a look at the entire plugin code and he found a function I had written which was forcing the query to only show 'Published' posts. This was interfering with the preview as it was being treated as a front-end query. I was able to bypass it by adding the parameter !$query->is_preview() to my conditions and it is now sorted.

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.