Claire
Claire

Reputation: 3785

Why are my custom posts in wordpress not displaying?

I create a post and then it says i has been published but when I click the url I get a page not found!

Here is my custom post set up code:

add_action('init', 'offered_register');
function offered_register() {

$labels = array(
    'name' => _x('Offered items'),
    'singular_name' => _x('Item'),
    'add_new' => _x('Give an item away'),
    'add_new_item' => __('Give a new item away'),
    'edit_item' => __('Edit item'),
    'new_item' => __('Give Item Away'),
    'view_item' => __('View Item'),
    'search_items' => __('Search Offered Items'),
    'not_found' =>  __('Nothing found'),
    'not_found_in_trash' => __('Nothing found in Trash'),
    'parent_item_colon' => ''
);

$args = array(
    'labels' => $labels,
    'public' => true,
    'publicly_queryable' => true,
    'show_ui' => true,
    'query_var' => true,
    'menu_icon' => get_stylesheet_directory_uri() . '/images/article16.png',
    'rewrite' => true,
    'capability_type' => 'post',
    'hierarchical' => false,
    'menu_position' => null,
    'supports' => array('title','thumbnail', 'custom-fields','comments')
  ); 

register_post_type( 'offered' , $args );
}

Upvotes: 2

Views: 4853

Answers (4)

Dishan TD
Dishan TD

Reputation: 8748

Anytime a new custom post type is registered the rewrite rules have to be flushed. Go to Settings -> Permalinks and click save.

Upvotes: 0

Rahul Dutta
Rahul Dutta

Reputation: 11

Your answer is:

Just change the Permalink Settings to default and save it and then again went for the post name..This will work.

Upvotes: 1

gradyetc
gradyetc

Reputation: 771

Your register_post_type() function looks fine. I'm guessing that you have pretty permalinks enabled. Visit the Settings > Permalinks page and try again.

Then read this: Flushing Rewrite Rules on Plugin Activation

The issue is that when a new post type is registered, the rewrite rules that WordPress uses to handle pretty permalinks do not automatically get re-generated. flush_rewrite_rules() must be called to pick up the new post type rewrites. This is commonly done in two ways:

  1. If you're working on a plugin, add flush_rewrite_rules() to the plugin activation hook callback (see the link above)
  2. Visit the Settings > Permalinks page, which calls flush_rewrite_rules() for you

What you do not want to do is call flush_rewrite_rules() in the init callback -- it is not something you want called on every page request.

Upvotes: 10

Jassi Oberoi
Jassi Oberoi

Reputation: 1424

use add_filter but not sure it works or not.

Upvotes: 0

Related Questions