Using WordPress ‘get_object_subtype_{$object_type}’ PHP filter

The get_object_subtype_{$object_type} WordPress PHP filter allows you to modify the object subtype identifier for non-standard object types.

Usage

add_filter('get_object_subtype_post', 'your_custom_function', 10, 2);

function your_custom_function($object_subtype, $object_id) {
    // Your custom code here
    return $object_subtype;
}

Parameters

  • $object_subtype: string – Empty string to override.
  • $object_id: int – ID of the object to get the subtype for.

More information

See WordPress Developer Resources: get_object_subtype_{$object_type}

Examples

Change subtype for a custom post type

In this example, the object subtype for a custom post type called ‘product’ will be changed to ‘custom_product_subtype’.

add_filter('get_object_subtype_post', 'change_product_subtype', 10, 2);

function change_product_subtype($object_subtype, $object_id) {
    if (get_post_type($object_id) == 'product') {
        $object_subtype = 'custom_product_subtype';
    }
    return $object_subtype;
}

Change user subtype based on user role

This example modifies the user subtype based on the user’s role.

add_filter('get_object_subtype_user', 'change_user_subtype_based_on_role', 10, 2);

function change_user_subtype_based_on_role($object_subtype, $object_id) {
    $user = get_user_by('ID', $object_id);
    if (in_array('administrator', $user->roles)) {
        $object_subtype = 'admin_subtype';
    } elseif (in_array('editor', $user->roles)) {
        $object_subtype = 'editor_subtype';
    }
    return $object_subtype;
}

Change term subtype for a specific taxonomy

This example changes the term subtype for a custom taxonomy called ‘location’.

add_filter('get_object_subtype_term', 'change_location_subtype', 10, 2);

function change_location_subtype($object_subtype, $object_id) {
    $term = get_term($object_id);
    if ($term->taxonomy == 'location') {
        $object_subtype = 'custom_location_subtype';
    }
    return $object_subtype;
}

Change comment subtype based on comment type

This example modifies the comment subtype based on the comment type.

add_filter('get_object_subtype_comment', 'change_comment_subtype_based_on_type', 10, 2);

function change_comment_subtype_based_on_type($object_subtype, $object_id) {
    $comment = get_comment($object_id);
    if ($comment->comment_type == 'review') {
        $object_subtype = 'review_subtype';
    }
    return $object_subtype;
}

Add a custom prefix to post subtypes

In this example, a custom prefix ‘my_prefix_’ is added to all post subtypes.

add_filter('get_object_subtype_post', 'add_prefix_to_post_subtype', 10, 2);

function add_prefix_to_post_subtype($object_subtype, $object_id) {
    $object_subtype = 'my_prefix_' . $object_subtype;
    return $object_subtype;
}