WordPress.org

Forums

[resolved] How can I create a custom post type with empty slug? (12 posts)

  1. aggressivex
    Member
    Posted 4 years ago #

    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)
        )
      );
    }
  2. wpua
    Member
    Posted 4 years ago #

    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.

  3. aggressivex
    Member
    Posted 4 years ago #

    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();
  4. helgatheviking
    Member
    Posted 4 years ago #

    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.

  5. winningsem
    Member
    Posted 4 years ago #

    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

  6. ryanve
    Member
    Posted 4 years ago #

    This seems to work:

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

    Output: example.com/headline/

    Slug removed—no extra slashes.

    Remember to flush as mentioned by @winningsem

  7. Aurélien Denis
    Member
    Posted 4 years ago #

    It doesn't seem to work for WordPress 3.2.1...

  8. ciantic
    Member
    Posted 3 years ago #

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

  9. synthview
    Member
    Posted 3 years ago #

    @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 :)

  10. synthview
    Member
    Posted 3 years ago #

    well, pages screw up with the / ...:(

  11. ryanve
    Member
    Posted 3 years ago #

    @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.

  12. ryanve
    Member
    Posted 3 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic