Function and usage of is_post_publicly_viewable() in wordpress


Answers ( 1 )


    The is_post_publicly_viewable() function in WordPress is designed to determine if a given post is publicly viewable. This function is particularly useful for theme and plugin developers who need to check the visibility of posts under various conditions.


    is_post_publicly_viewable( int|WP_Post|null $post = null ): bool


    The function checks whether a post is publicly viewable based on two key criteria:

    1. Post Status: The function evaluates the post's status to see if it is set to a status that is considered visible to the public, like 'publish'.
    2. Post Type: It also checks the post type to ensure it is one that is publicly queryable.


    • $post (int | WP_Post | null) (Optional): This parameter can be either the Post ID or a WP_Post object. If this parameter is not provided, the function will default to using the global $post object. This flexibility allows for checking the current post in a loop or a specific post by its ID.
      • Type: int, WP_Post, or null
      • Default: null


    • Type: bool
    • Value: Returns true if the post is publicly viewable, false otherwise.

    Sample Usage:

    1. Checking the Current Post in The Loop:

      if ( is_post_publicly_viewable() ) {
          // Do something if the current post in the loop is publicly viewable
    2. Checking a Specific Post by ID:

      $post_id = 123; // Assuming 123 is the post ID
      if ( is_post_publicly_viewable( $post_id ) ) {
          // Do something if the post with ID 123 is publicly viewable
    3. Checking Using a Post Object:

      $post = get_post( 123 ); // Retrieve the post object for post ID 123
      if ( is_post_publicly_viewable( $post ) ) {
          // Do something if the retrieved post is publicly viewable

    In each of these cases, the function provides a straightforward way to verify the visibility of a post, which is particularly useful in scenarios where content should only be displayed or acted upon if it is accessible to the public.

Leave an answer