1

I have created a custom post type called job. Posts are generated with wp_insert_post(), and that works perfectly fine. My problem is that the permalinks to those posts are either wrong or I have done something wrong myself. Basicaly the post heads to this url : http://myblog.eu/job/1-adjointe-de-direction-ee/ and it will display only if I go to http://myblog.eu/?job=1-adjointe-de-direction-ee/.

Id like each post to be generated with the right permalink.

Here is a random post array :

[9] => stdClass Object
        (
            [ID] => 42
            [post_author] => 1
            [post_date] => 2014-03-02 16:40:23
            [post_date_gmt] => 2014-03-02 15:40:23
            [post_content] => [reso_job_post id=8]
            [post_title] => adjoint(e) de direction, La Ciotat
            [post_excerpt] => 
            [post_status] => publish
            [comment_status] => closed
            [ping_status] => open
            [post_password] => 
            [post_name] => 8-adjointe-de-direction-la-ciotat
            [to_ping] => 
            [pinged] => 
            [post_modified] => 2014-03-02 16:40:23
            [post_modified_gmt] => 2014-03-02 15:40:23
            [post_content_filtered] => 
            [post_parent] => 0
            [guid] => http://myblog.eu/job/8-adjointe-de-direction-la-ciotat/
            [menu_order] => 0
            [post_type] => job
            [post_mime_type] => 
            [comment_count] => 0
        )

and here is the code I use to generate posts all variables are set right (as you can see above) :

$new_job_post = array(
                        'post_content' => $job_post_shortcode,
                        'post_title' => $job_post_title,
                        'post_status' => 'publish',
                        'comment_status' => 'closed',
                        'ping_status' => 'open',
                        'post_name' => $job_post_slug,
                        'post_type' => 'job',
                        'comment_count' => '0' 
                        );
                wp_insert_post( $new_job_post );

Here is the code I use to create the custom post type :

function job_custom_init() {
  $labels = array(
    'name'               => 'Jobs',
    'singular_name'      => 'Job',
    'add_new'            => 'Add New',
    'add_new_item'       => 'Add New Book',
    'edit_item'          => 'Edit Job',
    'new_item'           => 'New Job',
    'all_items'          => 'All Jobs',
    'view_item'          => 'View Job',
    'search_items'       => 'Search Jobs',
    'not_found'          => 'No jobs found',
    'not_found_in_trash' => 'No jobs found in Trash',
    'parent_item_colon'  => '',
    'menu_name'          => 'Jobs'
  );

  $args = array(
    'labels'             => $labels,
    'public'             => true,
    'publicly_queryable' => true,
    'show_ui'            => true,
    'show_in_menu'       => true,
    'query_var'          => true,
    'rewrite'            => array( 'slug' => 'job' ),
    'capability_type'    => 'post',
    'has_archive'        => true,
    'hierarchical'       => false,
    'menu_position'      => 5,
    'supports'           => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt' ),
    'taxonomies'         => array( 'category', 'post_tag' )
  );

  register_post_type( 'job', $args );
}
add_action( 'init', 'job_custom_init' );
function my_rewrite_flush() {
    job_custom_init();
    flush_rewrite_rules();
}
register_activation_hook( __FILE__, 'my_rewrite_flush' );

Thank you.

  • Show us the code you use to register your post type. – Milo Mar 29 '14 at 16:28
  • 1
    do you have pretty permalinks enabled and have you flushed permalinks after adding your post type? – Milo Mar 29 '14 at 17:45
  • Nope pretty perma link is not on. Concerning flushing, I have indeed although I'm not sure I've done that part right, I've edited the code above adding the flushing part at the end. And just for the record : I have added that part before posting this whole thread, I have also deactivated and reactivated the plugin since then as the trigger for the flushing is the activation – bastienbot Mar 29 '14 at 18:34
  • you need to have pretty permalinks enabled for this to work, that's your problem. – Milo Mar 29 '14 at 18:48
  • Alright so I made some research since I thought that pretty perma link was a plugin although its just the permalink setting. So actually permalinks are enabled using the article name as default. Please pardon my noobiness regarding wordpress, I just gfot into it a little while ago... So now could you tell me how i'd be able to override the permalink setting in my plugin ? Is it even possible ? Thank you ever so much for the time you're taking by the way, it's much appreciated ! – bastienbot Mar 29 '14 at 19:29
0

Try updating your permalinks 3 times. Make sure you have set it correct. Also you should look into http://wordpress.org/plugins/super-cpt/ , makes it a lot easier to add custom post types.

| improve this answer | |
  • why would updating permalinks 3 times be different from updating once? also note that simply loading the permalinks settings page flushes rewrite rules, you don't have to make any changes or save them. – Milo Mar 31 '14 at 20:20
  • Good question... but each time I install a plugin affecting the permalinks, updating the permalinks 3 times resolves any issues. I think it's because it forces WP to rewrite the htaccess file. – Nuro Mar 31 '14 at 20:39
  • Thanks Nuro, Ill give it a go if I get this issue again. I didnt know this plugin, it seemsvery powerful, thanks ! – bastienbot Mar 31 '14 at 20:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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