WordPress.org

Ready to get started?Download WordPress

Forums

custom post types - not found (or infinite loop) (7 posts)

  1. rapidz
    Member
    Posted 3 years ago #

    For some reason, wordpress is giving me real trouble with custom post types.

    I'm using the following code to register the custom post type:

    add_action('init', 'codex_custom_init');
    function codex_custom_init()
    {
    flush_rewrite_rules();
      $labels = array(
        'name' => _x('Communities', 'post type general name'),
        'singular_name' => _x('Community', 'post type singular name'),
        'add_new' => _x('Add New', 'community'),
        'add_new_item' => __('Add New Community'),
        'edit_item' => __('Edit Community'),
        'new_item' => __('New Community'),
        'view_item' => __('View Community'),
        'search_items' => __('Search Communities'),
        'not_found' =>  __('No communities found'),
        'not_found_in_trash' => __('No communities found in Trash'),
        'parent_item_colon' => '',
        'menu_name' => 'Communities'
    
      );
      $args = array(
        'labels' => $labels,
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'query_var' => true,
        'rewrite' => array('slug'=>'comm'),
        'capability_type' => 'post',
        'has_archive' => true,
        'hierarchical' => false,
        'menu_position' => null,
        'supports' => array('title','editor','author','thumbnail','excerpt','comments')
      );
      register_post_type('community',$args);
    }

    I then create a new post as the new custom post type, but when I try and access the post, I get the following:

    <strong>Not Found
    </strong>
    Apologies, but we were unable to find what you were looking for. Perhaps searching will help.

    I've also gone to the wordpress dashboard and into settings->permalinks, but when I do that, and navigate to the post, I get an infinite redirect loop.

    Any help would be much appreciated. I'm hoping I've made my point clear, but if I haven't, I can provide more details.

    Thanks in advance.

  2. markgia
    Member
    Posted 3 years ago #

    I'm having the exact same problem. I'm creating a plugin which requires a custom post type. The admin side of things is perfect but posts aren't showing. Even when I preview before publishing posts are not displayed.

    Have you managed to resolve this problem rapidz?

  3. Dion Hulse
    WordPress Dev
    Posted 3 years ago #

    First up, flush_rewrite_rules(); needs to be run AFTER the register_post_type() call, which is what's happening here.

    However, a BIG note here, flush_rewrite_rules(); should NEVER be run on the init hook, or on any hook which is run on pageloads. Instead, you should use the plugin activation hook to call it.

    So, either

    1. call flush_rewrite_rules(); on plugin activation
    2. Visit the Settings -> Permalinks page and click save
  4. markgia
    Member
    Posted 3 years ago #

    Thanks Dion,

    I followed your advice but I think my problem lies elsewhere.

    I have two plugin files - main.php and admin.php

    main.php includes admin.php when is_admin() == true and I call register_post_type(); inside admin.php

    My custom post type would work when I leave the $rewrite argument undeclared but assume I defined 'slug' => food_items the post would not be found. I tried changing permalink settings and calling flush_rewrite_rules(); but no luck.

    Eventually I placed the code in functions.php and hey presto! Everything worked as you would expect. Even changing the slug argument in $rewrite array worked everytime without having to mess with permalink settings.

    Rapidz are you declaring custom post types in your plugin? If so I would suggest moving the code over to a single file if you haven't done so already.

    Does anybody have any insight in to why custom post types aren't working when I create them in include files?

  5. markgia
    Member
    Posted 3 years ago #

    Its so plain to see now! I was limiting register_post_type(); to the admin dashboard and of course the template has to have access to the arguments as well to display it correctly!

    Oh things were so much simpler in C++ (never thought I'd ever say that)

  6. rapidz
    Member
    Posted 3 years ago #

    Sorry I didn't post back here earlier. The issue is now resolved.

    It would appear that my problem was a simple one. I had the 'advanced permalinks' plugin activated, which was clearly causing some kind of conflict within the htaccess file created by wordpress.

    I deactivated and deleted the plugin, as it wasn't needed anyway - and voila, all worked perfectly.

    TIP - One thing I will say is, make sure that when you're creating a custom content type, be sure to go to Settings -> Permalinks, to update the htaccess file.

  7. impervious
    Member
    Posted 2 years ago #

    @Dion - do you have a code sample? I've tried using flush_rewrite_rules in the activation hook, and it didn't work, and I really don't know why.

Topic Closed

This topic has been closed to new replies.

About this Topic