禁用一个自定义帖子类型的WordPress垃圾箱

时间:2017-02-28 18:57:21

标签: wordpress custom-post-type

是否可以为一个自定义帖子类型禁用WordPress垃圾回收功能?

目标是拥有与定义相同的功能(' EMPTY_TRASH_DAYS',0) - 永久删除邮件 - 除了一个CPT,而不是网站范围。

谢谢,

2 个答案:

答案 0 :(得分:3)

虽然我无法想出一个特别优雅的方法,但我可以使用wp_trash_post操作绕过垃圾箱。

<?php
function directory_skip_trash($post_id) {
    if (get_post_type($post_id) == 'directory') {
        // Force delete
        wp_delete_post( $post_id, true );
    }
} 
add_action('wp_trash_post', 'directory_skip_trash');

基本上当帖子被删除时,你会在$ force_delete参数设置为true的情况下再次删除它。

通过在此CPT的管理界面中找到更改“垃圾箱”一词的方法,可以改进此解决方案,但对于我的特定用例,这种方法效果很好。

答案 1 :(得分:0)

我无法执行wp_trash_post操作,因此我使用了另一种方法,将垃圾链接替换为删除链接。

post_row_actions过滤“帖子”列表表上的行操作链接数组。

<?php
function replace_trash_with_delete( $actions, $post ) {
    if( 'directory' === $post->post_type ){ // Replace 'directory' with your post type
        unset( $actions['trash'] ); // Removes the trash link 

        $deleteUrl = esc_url( get_delete_post_link( $post->ID, '', true ) ) ;
        $deleteLink = '<a rel="nofollow" href="' . $deleteUrl . '">' . __('Delete') .'</a>'; // Creates the new delete link

        $actions = 
            array_slice( $actions, 0, 1, true ) +
            array( 'delete' => $deleteLink ) +
            array_slice( $actions, 1, count( $actions ) - 1, true ); // Adds the delete link to the array of links
    }
} 
add_filter( 'post_row_actions','replace_trash_with_delete', 10, 2 );