Using WordPress ‘is_term_publicly_viewable()’ PHP function

The is_term_publicly_viewable() WordPress PHP function determines whether a term is publicly viewable based on its taxonomy.

Usage

is_term_publicly_viewable( $term )

Example:

Input: is_term_publicly_viewable( 12 )

Output: true or false

Parameters

  • $term (int|WP_Term) (Required) – Term ID or term object.

More information

See WordPress Developer Resources: is_term_publicly_viewable()

Examples

Check if a category term is publicly viewable

This example checks if a category term with the ID 10 is publicly viewable.

$category_id = 10;
$is_viewable = is_term_publicly_viewable( $category_id );

if ( $is_viewable ) {
    echo 'The category is publicly viewable.';
} else {
    echo 'The category is not publicly viewable.';
}

Check if a tag term is publicly viewable

This example checks if a tag term with the ID 20 is publicly viewable.

$tag_id = 20;
$is_viewable = is_term_publicly_viewable( $tag_id );

if ( $is_viewable ) {
    echo 'The tag is publicly viewable.';
} else {
    echo 'The tag is not publicly viewable.';
}

Check if a custom taxonomy term is publicly viewable

This example checks if a custom taxonomy term with the ID 30 is publicly viewable.

$custom_taxonomy_term_id = 30;
$is_viewable = is_term_publicly_viewable( $custom_taxonomy_term_id );

if ( $is_viewable ) {
    echo 'The custom taxonomy term is publicly viewable.';
} else {
    echo 'The custom taxonomy term is not publicly viewable.';
}

Check if a term object is publicly viewable

This example checks if a term object is publicly viewable.

$term_object = get_term( 40 );
$is_viewable = is_term_publicly_viewable( $term_object );

if ( $is_viewable ) {
    echo 'The term is publicly viewable.';
} else {
    echo 'The term is not publicly viewable.';
}

Display a list of viewable categories

This example retrieves all categories and displays only the ones that are publicly viewable.

$categories = get_categories();

foreach ( $categories as $category ) {
    if ( is_term_publicly_viewable( $category ) ) {
        echo 'Category: ' . $category->name . '<br>';
    }
}

Tagged in

Leave a Comment

Your email address will not be published. Required fields are marked *