0

I registered a new post type called "events". The posts of this type do show up in the loop but i can't access the single-events.php when i click on a "event" post. Also I can't access the categories of this post type.

The only advice you read on the internet - to flush the rewrite rules - didn't work me.

Any other suggestions?

Here is my registration code for this post type:

add_action ('init', 'register_events_posttype');
function register_events_posttype(){
   $labels = array();

    $args = array(
        'label' => 'Events',
        'labels' => $labels,
        'show_in_menu' => true,
        'show_ui' => true,
        'show_in_nav_menus' => true,
        'show_in_rest' => true,
        'menu_position' => 2,
        'menu_icon' => 'dashicons-calendar-alt',
        'supports' => array('title','editor','thumbnail', 'excerpt', 'custom-fields', 'comments','revisions', 'archives',),
        'taxonomies' => array('category', 'post_tag'),
        'rewrite' => array('slug' => 'events','with_front' => false)
    );
    register_post_type('event', $args);
}

3 Answers 3

2

according to your code, your cpt is "event". You will either need to change the your php to single-event.php or change this line:

register_post_type('event', $args);

to

register_post_type('events', $args); 

Usually they are the plural so the 2nd option is a better choice. With that being said, I always recommend adding namespace to avoid conflicts. Especially in this case because many people have event CPTs.

Try "shc-events" or something like that to make it your own.

4
  • Good point about naming conflicts.
    – Pat J
    Commented Feb 6, 2017 at 17:03
  • I had already a single-events.php file with the same content as inside single-event.php. But i changed the cpt(?) inside the register_post_type function. my whole site broke down and i had to change all the mentions of "event" (database, php and ajax/javascript files). but nothing changed: the single- and archive-event pages are still not available :( Commented Feb 6, 2017 at 18:11
  • Could you please put a bit more info up for me then? What do you mean your whole site broke? Are you just creating this site or trying to fix a CPT (Custom Post Type) that has already been in your site for a while? If you did change that line above as suggested, you may want. Try commenting out the "rewrite" line & see. The extra comma after archives as well. I know you know this, but remember to flush permalinks after each change because you don't have any code that's doing for you if that is all the code you wrote for the registration. If this doesn't work, i'll try it on my system for you.
    – rudtek
    Commented Feb 6, 2017 at 23:24
  • what I meant by saying "the whole site broke" was that my 'event' posts disappeared. so i had first to change the former post_type name from "event" to "events" inside the database. But than i was still missing a lot of features i already have build (i'm working on my own theme from scratch). So now i hat to search among some hundreds of matches for the right "event" to change it to "events" inside my .php files and ajax calls :) But now i figured out what the problem was: wordpress.stackexchange.com/a/255454/80177 Commented Feb 7, 2017 at 0:52
1

You've registered the post type event, not events. So you should be able to use single-event.php. (Or, alternately, change your last line to register_post_type( 'events', $args );.

Docs: register_post_type()

1
  • I had already a single-events.php file with the same content as inside single-event.php. But i changed the cpt(?) inside the register_post_type function. my whole site broke down and i had to change all the mentions of "event" (database, php and ajax/javascript files). but nothing changed: the single- and archive-event pages are still not available :( Commented Feb 6, 2017 at 18:11
0

Hmm. After spending hours on research and trying out some suggested solutions I found out I just needed to add this arguments to the arguments array and only then to flush the permalinks (= switching to the default permalink structure and back to custom post name structure in the dashboard settings):

'public' => true,
'has_archive' => true,

It had nothing to do with the singular parameter ('event') in register_post_type. I changed it back like it was (singular 'event') because i like to have it singular for the database requests. The post type name for posts inside the database is singular as well ('post').

But for the WP REST API route I set the rest_base to plural:

'rest_base' => 'events',

Your Answer

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

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