Support » Fixing WordPress » Create Custom Posttype with no permalink

  • Resolved Xeevee


    Hi all,

    I’ve been playing around with theme development for a little while now and have been creating a settings page to store information such as social media links, contact details etc …

    However this requires me to have already hard coded the Social Media fields for the settings page.

    Ideally I would like to create a Social Media Post type and then the user can add as many to the list as they like, and I just run through a loop of the post type where I want to print the links.

    The problem is I don’t want this post type to have a Permalink, I don’t want people to be able to find a “Facebook” page on the site and all that’s on it is a post title. Now I know I could set up a 301 for the custom post type eg: “/socialmedia/” in my htaccess file, but ideally I would just like to remove the Permalink from the post type all together.

    I’ve had a Google around for any tips but not found anything yet, If anyone could help me out I’d greatly appreciate it. =)

    I’m pretty competent with WordPress so I’m not asking for a complete solution, a pointer in the right direction or just some suggested functions would probably be enough to get me going =)

    Thanks in advanced!

    – Jack

Viewing 3 replies - 1 through 3 (of 3 total)
  • I was going to say that all posts (and post types) MUST have a permalink, except … well, MENUS are CPTs. You could build off that functionality, in theory. Haven’t a clue how, but if it’s been done before, maybe you can start there.

    Hi Ipstenu,

    Thanks for your hasty reply, as you’ve said Menu’s are technically just custom post types, along with attachments, and a few others, and while I didn’t think there were Permalink’s associated with each of these types, after having a snoop around in the database they all have a guid set, however after trying to follow the links in the database for Menu items for example, it returns the default 404 template.

    After looking into this a little further I suspect the 301 redirects may indeed be the best alternative after all, just with some added code to hide the Permalink display on the edit post page.

    I’m going to continue to have a play around, and will post back any findings here for anyone that’s interested.

    Any further advice is still appreciated.

    – Jack

    For anyone who may be interested in this problem, I’ve come up with a solution, it’s not ideal, but it gets the job done.

    First I created my custom post type of the type “socialmedia”

    Then in my themes functions.php I’ve added the following function:

     * Removes the permalinks display on the socialmedia post type
    add_action('admin_init', 'remove_permalink');
    function remove_permalink() {
    	if($_GET['post']) {
    		$post_type = get_post_type($_GET['post']);
    		if($post_type == 'socialmedia' && $_GET['action'] == 'edit') {
    			echo '<style>#edit-slug-box{display:none;}</style>';

    This simply checks if you’re on the edit page of a post of type “socialmedia” and if so, sets the Permalink’s containing div to display none.

    Next, I create a file “single-socialmedia.php” which will be used by all posts of the type “socialmedia” as explained in the template hierarchy, and this file simply calls the 404.php file in my theme using the following code:

     * The Template for displaying all posts of the "socialmedia" type.
     *	- Loads the 404.php template
     * @package WordPress
     * @subpackage Project Bee Racing
     * @since Project Bee Racing 1.0
    <?php get_template_part( '404' ) ?>

    The end result is a post type that gives the illusion of having no permalink and is inaccessible on your site as an individual post. Perfect for creating item lists of an unknown quantity / value social as social media links.

    – Jack

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Create Custom Posttype with no permalink’ is closed to new replies.