0
// register custom post type forums
function fp_forum_post_type() {
    // set up labels
    $labels = array(
        'name' => 'Forums',
        'singular_name' => 'Forum',
        'add_new' => 'Add New Forum',
        'add_new_item' => 'Add New Forum',
        'edit_item' => 'Edit Forum',
        'new_item' => 'New Forum',
        'all_items' => 'All Forums',
        'view_item' => 'View Forum',
        'search_items' => 'Search Forums',
        'not_found' =>  'No Forums Found',
        'not_found_in_trash' => 'No Forums found in Trash', 
        'parent_item_colon' => 'Category:',
        'menu_name' => 'Forums',
    );
    //register post type
    register_post_type( 'forums', array(
        'labels' => $labels,
        'has_archive' => true,
        'public' => true,
        'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail','page-attributes' ),
        'exclude_from_search' => false,
        'capability_type' => 'post',
        'query_var' => true,
        'rewrite' => array( 'slug' => 'forums' ),
        )
    );

}

For some reason when I create a new post it duplicates the same slug as the first post, so say the slug is forums=hello-world, the next post will automatically be forums=hello-world-2 no matter what the title is...

I have tested on fresh install several times and I get the same issue...

UPDATE: I have discovered that the problem is being caused by my custom metabox:

function add_forum_attributes_metabox(){
add_meta_box( 
    'forum_attributes', 
    __('Forum Attributes', 'forumpress'), 
    'forum_attributes', 
    'forums', 
    'side', 
    'high', 
    array( 'id' => 'forum_attributes') 
);

}

function save_forum_attributes_metabox($post_id, $post){
// Don't wanna save this now, right?

if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
    return;
if ( !isset( $_POST['forum_attributes_nonce'] ) )
    return;
if ( !wp_verify_nonce( $_POST['forum_attributes_nonce'], 'forumpress/includes/metaboxes/metaboxes.php' ) )
    return;

// We do want to save? Ok!
$key = 'forum_type';
$value = $_POST["forum_type"];
if ( get_post_meta( $post->ID, $key, FALSE ) ) { // If the custom field already has a value
    update_post_meta( $post->ID, $key, $value );
} else { // If the custom field doesn't have a value
    add_post_meta( $post->ID, $key, $value );
}
if ( !$value ) delete_post_meta( $post->ID, $key ); // Delete if blank

$key = 'forum_category';
$value = $_POST["forum_category"];
if ( get_post_meta( $post->ID, $key, FALSE ) ) { // If the custom field already has a value
    update_post_meta( $post->ID, $key, $value );
} else { // If the custom field doesn't have a value
    add_post_meta( $post->ID, $key, $value );
}
if ( !$value ) delete_post_meta( $post->ID, $key ); // Delete if blank

$key = 'forum_status';
$value = $_POST["forum_status"];
if ( get_post_meta( $post->ID, $key, FALSE ) ) { // If the custom field already has a value
    update_post_meta( $post->ID, $key, $value );
} else { // If the custom field doesn't have a value
    add_post_meta( $post->ID, $key, $value );
}
if ( !$value ) delete_post_meta( $post->ID, $key ); // Delete if blank

$key = 'forum_order';
$value = $_POST["forum_order"];
if ( get_post_meta( $post->ID, $key, FALSE ) ) { // If the custom field already has a value
    update_post_meta( $post->ID, $key, $value );
} else { // If the custom field doesn't have a value
    add_post_meta( $post->ID, $key, $value );
}
if ( !$value ) add_post_meta( $post->ID, $key, '0' ); // add 0 if blank

}

  • So you're creating 2 posts with the same title and expect them to have the same slug? – Tom J Nowell Sep 27 '17 at 21:34
  • No, I'm creating two posts with completely different names and for some reason WordPress is setting the slugs the same as the first one, only adding a 2 on the end... – lukgoh Sep 27 '17 at 21:58
  • 1
    And these are the default self assigned slugs? Or are you manually setting slugs? I've never encountered this, this is pure vanilla WordPress core with a default theme and no plugins? – Tom J Nowell Sep 27 '17 at 22:50
  • Yeah WordPress is default setting them itself. Yeah, I'm only running this plugin. I also have two other custom post types set up the same and they work fine – lukgoh Sep 27 '17 at 22:57
0

SOLVED:

Silly mistake that I made was using wp_query inside the custom metabox output...

face palm

If you found this post googling the same issue, you need to use a different query method as it conflicts with the page.

Using this:

$query = get_posts( array( 'post_type' => 'forums', 'post_status' => 'publish' ) );

fixed this problem.

|improve this answer|||||

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.