【问题标题】:My custom post type redirect to edit.php after publish and update post in wordpress在 wordpress 中发布和更新帖子后,我的自定义帖子类型重定向到 edit.php
【发布时间】:2014-05-23 06:00:32
【问题描述】:

我创建了新的自定义帖子类型 - 在 wordpress 中进行交易,并在发布新交易帖子后重定向到 edit.php(帖子列表)。所以我想把它重定向到交易列表页面(http://mydemo.com/demo/edit.php?post_type=deals)所以请帮助我。

我试过这段代码,但它会将所有帖子类型重定向到指定页面。

add_filter( 'redirect_post_location', 'wpse_124132_redirect_post_location' );
/**
 * Redirect to the edit.php on post save or publish.
 */


function wpse_124132_redirect_post_location( $location ) {

    if ( isset( $_POST['save'] ) || isset( $_POST['publish'] ) )
        return admin_url( "edit.php?post_type=deals" );

    return $location;
} 

【问题讨论】:

    标签: php wordpress redirect


    【解决方案1】:

    你需要使用get_post_type()函数。

    将代码更改为

    function wpse_124132_redirect_post_location( $location ) {
    
        if ( 'deals' == get_post_type() ) {
    
        /* Custom code for 'deals' post type. */
    
            if ( isset( $_POST['save'] ) || isset( $_POST['publish'] ) )
                return admin_url( "edit.php?post_type=deals" );
    
        } 
        return $location;
    } 
    

    编辑:由于您在循环之外使用get_post_type(),因此您需要传递帖子的ID,

    get_post_type($_POST['id'])

    【讨论】:

    • 它就像一个魅力。谢谢。我觉得很奇怪,当其他非管理员用户无权访问本机帖子时,WP 会将他们重定向到 edit.php。我也不明白为什么 WP 不提高权限,让我大吃一惊的是,用户必须拥有“edit_posts”才能在管理后端执行任何操作。
    猜你喜欢
    • 1970-01-01
    • 2020-10-24
    • 1970-01-01
    • 2016-11-25
    • 2016-11-29
    • 2016-04-27
    • 1970-01-01
    • 2017-05-21
    • 1970-01-01
    相关资源
    最近更新 更多