用于新帖子的自定义帖子类型复制插件

时间:2017-09-27 作者:lukgoh

// 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\' ),
        )
    );

}
由于某种原因,当我创建一篇新帖子时,它会复制与第一篇帖子相同的slug,所以如果slug是forums=hello world,那么无论标题是什么,下一篇帖子都会自动是forums=hello-world-2。。。

我已经在fresh install上测试了好几次,我也遇到了同样的问题。。。

更新:我发现问题是由我的自定义元数据库引起的:

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
}

1 个回复
最合适的回答,由SO网友:lukgoh 整理而成

SOLVED:

我犯了一个愚蠢的错误,在自定义metabox输出中使用wp\\u查询。。。

面部手掌

如果你发现这篇文章在谷歌上搜索同一个问题,你需要使用不同的查询方法,因为它与页面冲突。

使用此选项:

$query = get_posts( array( \'post_type\' => \'forums\', \'post_status\' => \'publish\' ) );
已修复此问题。

结束

相关推荐

Post & Page with same slug

我有一篇帖子和一个页面,上面写着这个鼻涕虫:signatures-for-office-365 没错,帖子和页面也是如此。Permalinks settings are: /news/%postname%/当请求post URL时,wp会将其转到页面,如下所示:Requested:/news/signatures-for-office-365Result:/signatures-for-office-365我试过这个:add_action(\'init\', function () { add_