WordPress 5.7 introduces a function to check whether an article can be viewed publicly

WordPress 5.7 Two functions are introduced to check whether the article can be viewed publicly。

is_post_status_viewable()

before, is_post_type_viewable() Function can be used to register article types by publicly_queryable Setting determines whether the article type is visible to anonymous users 。however,It’s not enough just to determine if you can view specific articles,Because this function only checks for general article type settings。

This is it.WordPress 5.7introduceis_post_status_viewable()The reason for the function,This function enables developers to determine whether the status of an article is publicly viewable。Internal and protected states are always considered invisible。For built-in article status, Will check public attribute,For custom state,Will check publicly_queryable attribute。

is_post_status_viewable() Function takes an argument:

  • $post_status:Article status name or object。This parameter is required。

Examples of usage:

 global $post;
 $current_post_status = get_post_status( $post );
 if ( is_post_status_viewable( $current_post_status ) ) {
     echo 'This post uses a public post status';
 } else {
     echo 'This post uses a non public post status';
 }

Please note that,Password protected articles are considered publicly visible,Private articles are invisible。

is_post_publicly_viewable()

WordPress 5.7 还introduce is_post_publicly_viewable() ,Used to determine whether outgoing users can view individual articles。If bothis_post_status_viewable()and is_post_type_viewable()return true articleattribute,The author thinks that the article can be seen 。

is_post_publicly_viewable() Function takes an argument:

  • $post:articleID或articleobject。This parameter is optional。By default,It passes the whole picture$postobject。

Examples of usage:

if ( is_post_publicly_viewable() ) {
    echo 'This post is publicly viewable';
} else {
    echo 'This post is not publicly viewable';
}

in addition,this is_post_type_viewable() Function has been modified to,在将未注册的article类型传递给该函数时return false ,以避免尝试访问非object(non-object)上的attribute。

To learn more,Please refer to the work order #49380

Related Article
The first release candidate for WordPress 5.7 is now available!

You can test the WordPress 5.7 release candidate in two ways: Try the WordPress Beta Tester plugin (choose the “Bleeding edge” channel Read more

WordPress automatically intercepts part of the content of the article as a summary

In the daily release of articles, not every article may have an article summary, so the summary function of WordPress Read more

How to add user defined upload avatar function in WordPress

Friends who use WordPress to build a website should know that WordPress itself does not have the function of uploading Read more

How to get the number of articles and comments of the current logined user in WordPress

When developing WordPress Theme user center, I want to get the number of articles and comments of current logged in Read more

Leave a Reply

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

*
To prove you're a person (not a spam script), type the security word shown in the picture.
Anti-spam image