1

I'm trying to separate out my marketing landing pages (squeeze pages) into separate post types to make organizing them easier as well as to make it easier to interact with some of my plugins. I have it working right now after generating the custom post type via GenerateWP, and I've modified the permalink to remove the post type slug by leveraging the snippet here. Automatically-generated links to the new post type appear to use the unmodified URL, which includes the post_type, and which now resolves to a 404. An example is the "View" link in the list of items in the custom post type list screen.

Is there a way to permanently alter the permalink, such that any references to the post generate the same URL? Here's what I have right now:

if ( ! function_exists('bh_squeeze_page') ) {

    // Register Custom Post Type
    function bh_squeeze_page() {

        $labels = array(
            'name'                  => _x( 'Squeeze Pages', 'Post Type General Name', 'text_domain' ),
            'singular_name'         => _x( 'Squeeze Page', 'Post Type Singular Name', 'text_domain' ),
            'menu_name'             => __( 'Squeeze Pages', 'text_domain' ),
            'name_admin_bar'        => __( 'Squeeze Page', 'text_domain' ),
            'archives'              => __( 'Squeeze Page Archives', 'text_domain' ),
            'attributes'            => __( 'Squeeze Page Attributes', 'text_domain' ),
            'parent_item_colon'     => __( 'Parent Squeeze Page:', 'text_domain' ),
            'all_items'             => __( 'All Squeeze Pages', 'text_domain' ),
            'add_new_item'          => __( 'Add New Squeeze Page', 'text_domain' ),
            'add_new'               => __( 'Add New', 'text_domain' ),
            'new_item'              => __( 'New Squeeze Page', 'text_domain' ),
            'edit_item'             => __( 'Edit Squeeze Page', 'text_domain' ),
            'update_item'           => __( 'Update Squeeze Page', 'text_domain' ),
            'view_item'             => __( 'View Squeeze Page', 'text_domain' ),
            'view_items'            => __( 'View Squeeze Pages', 'text_domain' ),
            'search_items'          => __( 'Search Squeeze Page', 'text_domain' ),
            'not_found'             => __( 'Not found', 'text_domain' ),
            'not_found_in_trash'    => __( 'Not found in Trash', 'text_domain' ),
            'featured_image'        => __( 'Featured Image', 'text_domain' ),
            'set_featured_image'    => __( 'Set featured image', 'text_domain' ),
            'remove_featured_image' => __( 'Remove featured image', 'text_domain' ),
            'use_featured_image'    => __( 'Use as featured image', 'text_domain' ),
            'insert_into_item'      => __( 'Insert into item', 'text_domain' ),
            'uploaded_to_this_item' => __( 'Uploaded to this item', 'text_domain' ),
            'items_list'            => __( 'Items list', 'text_domain' ),
            'items_list_navigation' => __( 'Items list navigation', 'text_domain' ),
            'filter_items_list'     => __( 'Filter items list', 'text_domain' ),
        );
        $rewrite = array(
            'slug'                  => 'sp',
            'with_front'            => false,
            'pages'                 => true,
            'feeds'                 => true,
        );
        $args = array(
            'label'                 => __( 'Squeeze Page', 'text_domain' ),
            'description'           => __( 'Squeeze Pages', 'text_domain' ),
            'labels'                => $labels,
            'supports'              => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'trackbacks', 'revisions', 'custom-fields', 'page-attributes', 'post-formats', ),
            'taxonomies'            => array( 'category' ),
            'hierarchical'          => true,
            'public'                => true,
            'show_ui'               => true,
            'show_in_menu'          => true,
            'menu_position'         => 5,
            'show_in_admin_bar'     => true,
            'show_in_nav_menus'     => true,
            'can_export'            => true,
            'has_archive'           => false,
            'exclude_from_search'   => false,
            'publicly_queryable'    => true,
            'rewrite'               => $rewrite,
            'capability_type'       => 'page',
            'show_in_rest'          => false,
            'rest_base'             => 'bh_squeeze_page',
        );
        register_post_type( 'bh_squeeze_page', $args );

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

    function na_remove_slug( $post_link, $post, $leavename ) {

    if ( 'bh_squeeze_page' != $post->post_type || 'publish' != $post->post_status ) {
      return $post_link;
    }

    $post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );

    return $post_link;
    }
    add_filter( 'post_type_link', 'na_remove_slug', 10, 3 );

    function na_parse_request( $query ) {

    if ( ! $query->is_main_query() || 2 != count( $query->query ) || ! isset( $query->query['page'] ) ) {
      return;
    }

    if ( ! empty( $query->query['name'] ) ) {
      $query->set( 'post_type', array( 'post', 'bh_squeeze_page', 'page' ) );
    }
    }
    add_action( 'pre_get_posts', 'na_parse_request' );

}
2
  • The post_type_link filter in your code is what modifies the URL generated by WordPress. Right now it's hardcoded to modify only the bh_squeeze_page post type.
    – Milo
    Jun 2, 2017 at 22:46
  • @Milo right, that's what I want. I only want to modify the permalink for this one post type.
    – bhamrick
    Jun 2, 2017 at 23:14

1 Answer 1

1

This line:

$post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );

isn't doing what you expect because your post slug isn't $post->post_type, it's sp, as set here:

$rewrite = array(
    'slug'        => 'sp',
    'with_front'  => false,
    'pages'       => true,
    'feeds'       => true,
);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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