Menü schliessen
Created: September 16th 2021
Last updated: December 10th 2021
Categories: Common Web Development,  Php,  Wordpress
Author: Tim Fürer

WordPress: Disable Posts in Post Table by Condition

Tags:  PHP,  wordpress
Donation Section: Background
Monero Badge: QR-Code
Monero Badge: Logo Icon Donate with Monero Badge: Logo Text
82uymVXLkvVbB4c4JpTd1tYm1yj1cKPKR2wqmw3XF8YXKTmY7JrTriP4pVwp2EJYBnCFdXhLq4zfFA6ic7VAWCFX5wfQbCC

Do you need to disable specific posts in a WordPress admin post table? Use the following function to do so.


The Code

This is the code you need. Add it your functions.php:

function disable_posts_in_table($caps, $cap, $user_id, $args) {
    // If this condition is true, do nothing
    if ( $cap !== 'edit_post' && $cap !== 'delete_post' ) {
        return $caps;
    }

    // If this condition is true, disable the post
    if ( disabling_condition ) {
        $caps[] = 'do_not_allow';
    }

    return $caps;
}
add_filter( 'map_meta_cap', 'disable_posts_in_table', 10, 4 );

HOW TO USE

Replace the "disabling_condition" located in the function. If the condition is true, the post will be disabled inside the post table.