Using WordPress ‘get_post_status’ PHP filter

The get_post_status WordPress PHP filter allows you to modify the post status before it’s returned by the get_post_status() function.

Usage

add_filter('get_post_status', 'your_function_name', 10, 2);

function your_function_name($post_status, $post) {
    // your custom code here
    return $post_status;
}

Parameters

  • $post_status (string): The current post status.
  • $post (WP_Post): The post object for which the status is being retrieved.

More information

See WordPress Developer Resources: get_post_status

Examples

Change post status to private for a specific category

Change the post status to ‘private’ if the post belongs to a specific category.

add_filter('get_post_status', 'change_status_to_private', 10, 2);

function change_status_to_private($post_status, $post) {
    if (in_category('private', $post)) {
        return 'private';
    }
    return $post_status;
}

Change post status to draft for older posts

Change the post status to ‘draft’ if the post is older than 30 days.

add_filter('get_post_status', 'older_posts_to_draft', 10, 2);

function older_posts_to_draft($post_status, $post) {
    $post_age = (time() - strtotime($post->post_date)) / DAY_IN_SECONDS;

    if ($post_age > 30) {
        return 'draft';
    }
    return $post_status;
}

Force post status to pending for specific user role

Change the post status to ‘pending’ if the post author has the ‘subscriber’ role.

add_filter('get_post_status', 'subscriber_posts_pending', 10, 2);

function subscriber_posts_pending($post_status, $post) {
    $author = get_userdata($post->post_author);

    if (in_array('subscriber', $author->roles)) {
        return 'pending';
    }
    return $post_status;
}

Set post status to future for posts with a future publish date

Set the post status to ‘future’ if the post has a future publish date.

add_filter('get_post_status', 'future_posts_status', 10, 2);

function future_posts_status($post_status, $post) {
    if (strtotime($post->post_date) > time()) {
        return 'future';
    }
    return $post_status;
}

Change post status to draft for posts with no title

Change the post status to ‘draft’ if the post has no title.

add_filter('get_post_status', 'no_title_draft_status', 10, 2);

function no_title_draft_status($post_status, $post) {
    if (empty($post->post_title)) {
        return 'draft';
    }
    return $post_status;
}