Support » Plugins and Hacks » Hacks » How can I create a custom post type with empty slug?

  • Resolved aggressivex

    (@aggressivex)


    I’m working with custom post types, I’ve developed a plugin with no problems. the problem is when I try to set a empty slug. The permalink is created correctly but I always get 404 error with this URL.

    http://www.domain.com/test/post-name/ this works fine.
    http://www.domain.com/post-name/ this get 404 , but this is that I want.

    I’m wondering if it is possible to obtain this config, without touching the core of WP and make it forward compatible

    Thanks!

    function create_post_type() {
      register_post_type( 'test',
        array(
          'labels' => array(
            'name' => __( 'test' ),
            'singular_name' => __( 'tests' )
          ),
          'public' => true,
          'rewrite' => array(
          	'slug' => '',
    	    'with_front' => true)
        )
      );
    }
Viewing 11 replies - 1 through 11 (of 11 total)
  • Hi,

    register_post_type function build permalink with slug/post_type structure.
    so setting slug to ” will not help.

    WP need to know post type from query to show post.
    You can try hook some of query parsing actions/filters, check by post slug thats your custom post and add query param post_type to wp query before it will get post from db.

    Thanks 🙂

    I’ve solved it.

    This works, but I had to re-update the permalinks config to see the effect.

    <?php
    /*
    Plugin Name: Aggressivex Network Custom Post
    Version: 1.0
    Author: Luis Muñoz
    Author URI: http://www.aggressivex.com/author/luis.munoz/
    */
    class AN_custom_post
    {
    	var $post_type_name		= 'product';
    	var $post_type_label 		= 'Products';
    
    	function __construct()
    	{
    		add_action('init', array(&$this,'register_post'));
    	}
    
    	function register_post()
    	{
    		register_post_type($this->post_type,
    			array(
    				'label'           => $this->post_type_label,
    				'public'          => true,
    				'show_ui'         => true,
    				'hierarchical'	  => true,
    				'rewrite'         => array(
    					'slug'		 	=> '',
    					'with_front'	=> true
    				),
    				'hierarchical'    => true,
    				'supports'        => array(
    					'title',
    					'editor',
    					'custom-fields'
    				),
    			)
    		);
    	}
    }
    
    new AN_custom_post();

    that’s really neat! if i set the permalinks to /%postname%/ then it goes http://www.mysite.com/product1 woo. wish WP could automatically take the page-style permalinks so that blogs could maintain dates, but still that is a very cool work-around.

    Sometimes it doesn’t work unless you update your permalinks under settings > permalinks > Save Changes (Even if you didnt make any changes to the permalink in that page) it refresh the mod rewrite and get your new permalinks

    This seems to work:

    'rewrite' => array('slug' => '/')

    Output: example.com/headline/

    Slug removed—no extra slashes.

    Remember to flush as mentioned by @winningsem

    It doesn’t seem to work for WordPress 3.2.1…

    I get 404 if I put '/' to the slug in 3.2.1, how to do this now then?

    @ciantic

    have you tried to update permalinks after adding the / ?
    it worked for me doing that.
    The strange thing is un my virtual server I it worked directly, without having to do it.
    Don’t ask me why 🙂

    well, pages screw up with the / …:(

    @ciantic Yea you definitely need to flush the permalinks. To do that just go Setting » Permalinks and then try the page again. You don’t need to actually change anything on the Permalinks settings. Just going in there will flush them.

    @synthview Screw up how? I just tested this again with a current version of WP and it works no problem. Here is a live example I just put up from work.

    Actually I stand corrected. It blows sh** up LOL. The CPT worked but it seems to screw up some other things like the normal pages and the author pages. If you don’t want a slug my best suggestion for now is just to pick something really short as the slug or maybe an underscore. I think a slug is good anyway for users. If you’re getting 404s just change the slug and flush the permalinks again.

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘How can I create a custom post type with empty slug?’ is closed to new replies.