PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, March 3, 2022

[FIXED] Wordpress register_post_type Invalid post type

 March 03, 2022     php, wordpress     No comments   

Issue

I am creating my own posts in wordpress however I have hit a little problem and im not sure how to fix it.

The below register_post_type creates an Invalid post type error.

add_action( 'init', 'create_post_type' );
function create_post_type() {
register_post_type( 'talent',
    array(
        'labels' => array(
            'name' => __( 'Talent' ),
            'singular_name' => __( 'talent' )
        ),
    'public' => true,
    'has_archive' => true,
    )
);
}

it doesn't seem to like the word talent. If I change 'talent' to 'arist' it works. However it needs to be 'talent' for the URL. Ive checked on wordpress and using talent shouldn't cause any conflicts with default wordpress settings.


Solution

That's indeed quite odd, I tried your snippet which is working as expected. That makes me think probably you are trying already to register the same post type before through another method. It could be either via plugin, or in the same function.php file, or using a plugin to create custom types on the database (like CPT UI for example).

I suggest you both of the following:

  • Track down the real nature of the problem, in case it's not messing up something else for you. You probably might encounter another similar problem in the future which will surely waste your precious time.
  • Avoid general namespaces especially with global shared stuff like the custom post types, I'll generally call them projectname_postname so, for example, you'll end with something like this: stackoverflow_talent

Assumed you go for the second one, just use the slug rewrite for that particular post type in this way:

register_post_type( 'myproject_talent',
    array(
        'labels' => array(
            'name' => __( 'Talent' ),
            'singular_name' => __( 'talent' )
        ),
    'public' => true,
    'has_archive' => true,
    'rewrite' => array('slug' => 'talent'),
    )
);

Now all your posts will show the /talent/ path. Remember to flush the permalink structure after you create the post type, otherwise you cannot see that on the frontend.

Hope solves your problem.



Answered By - MacK
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing