最新公告
  • 欢迎您光临站盟网(原知事网),一个优质的网站源码基地、精品网站模板和插件。欢迎加入永久SVIP
  • WordPress函数wp_count_posts()用法 获取各个类型的文章总数

    正文概述 知事网   2020-09-17 10:09   615

    描述:

    获取各个类型的文章总数

    用法:

    <?php wp_count_posts( $type, $perm ); ?>

    参数:

    $type
    
    (string) (可选) wp_posts 中文章的类型,用于核查哪种类型与post_type对应。默认post。
    
    默认值: 'post'
    
    $perm
    
    (string) (可选) 如果该参数设置为'readable'并且用户已登录,则可将私密文章状态算入文章状态中。默认为empty string。
    
    默认值: empty string

    示例:

    获取发表状态文章的计数
    要获取已发表文章的状态类型,应先调用wp_count_posts()函数然后确查看“publish”属性。
    
    <?php
    $count_posts = wp_count_posts();
    $published_posts = $count_posts->publish;
    ?>

    源文件:

    /**
    * Count number of posts of a post type and if user has permissions to view.
    *
    * This function provides an efficient method of finding the amount of post's
    * type a blog has. Another method is to count the amount of items in
    * get_posts(), but that method has a lot of overhead with doing so. Therefore,
    * when developing for 2.5+, use this function instead.
    *
    * The $perm parameter checks for 'readable' value and if the user can read
    * private posts, it will display that for the user that is signed in.
    *
    * @since 2.5.0
    *
    * @global wpdb $wpdb
    *
    * @param string $type Optional. Post type to retrieve count. Default 'post'.
    * @param string $perm Optional. 'readable' or empty. Default empty.
    * @return object Number of posts for each status.
    */
    function wp_count_posts( $type = 'post', $perm = '' ) {
    global $wpdb;
    
    if ( ! post_type_exists( $type ) )
    return new stdClass;
    
    $cache_key = _count_posts_cache_key( $type, $perm );
    
    $counts = wp_cache_get( $cache_key, 'counts' );
    if ( false !== $counts ) {
    /** This filter is documented in wp-includes/post.php */
    return apply_filters( 'wp_count_posts', $counts, $type, $perm );
    }
    
    $query = "SELECT post_status, COUNT( * ) AS num_posts FROM {$wpdb->posts} WHERE post_type = %s";
    if ( 'readable' == $perm && is_user_logged_in() ) {
    $post_type_object = get_post_type_object($type);
    if ( ! current_user_can( $post_type_object->cap->read_private_posts ) ) {
    $query .= $wpdb->prepare( " AND (post_status != 'private' OR ( post_author = %d AND post_status = 'private' ))",
    get_current_user_id()
    );
    }
    }
    $query .= ' GROUP BY post_status';
    
    $results = (array) $wpdb->get_results( $wpdb->prepare( $query, $type ), ARRAY_A );
    $counts = array_fill_keys( get_post_stati(), 0 );
    
    foreach ( $results as $row ) {
    $counts[ $row['post_status'] ] = $row['num_posts'];
    }
    
    $counts = (object) $counts;
    wp_cache_set( $cache_key, $counts, 'counts' );
    
    /**
    * Modify returned post counts by status for the current post type.
    *
    * @since 3.7.0
    *
    * @param object $counts An object containing the current post_type's post
    * counts by status.
    * @param string $type Post type.
    * @param string $perm The permission to determine if the posts are 'readable'
    * by the current user.
    */
    return apply_filters( 'wp_count_posts', $counts, $type, $perm );
    }

    站盟网 » WordPress函数wp_count_posts()用法 获取各个类型的文章总数

    发表评论

    还没有评论,快来抢沙发吧!

    如需帝国cms功能定制以及二次开发请联系我们

    联系作者
    请选择支付方式
    ×
    支付宝支付
    微信支付
    余额支付
    ×
    微信扫码支付 0 元