wp_count_posts example



/** * Returns all the possible statuses for a post type. * * @since 2.5.0 * * @param string $type The post_type you want the statuses for. Default 'post'. * @return string[] An array of all the statuses for the supplied post type. */
function get_available_post_statuses( $type = 'post' ) {
    $stati = wp_count_posts( $type );

    return array_keys( get_object_vars( $stati ) );
}

/** * Runs the query to fetch the posts for listing on the edit posts page. * * @since 2.5.0 * * @param array|false $q Optional. Array of query variables to use to build the query. * Defaults to the `$_GET` superglobal. * @return array */
$post_type = $this->screen->post_type;
        $per_page  = $this->get_items_per_page( 'edit_' . $post_type . '_per_page' );

        /** This filter is documented in wp-admin/includes/post.php */
        $per_page = apply_filters( 'edit_posts_per_page', $per_page$post_type );

        if ( $this->hierarchical_display ) {
            $total_items = $wp_query->post_count;
        } elseif ( $wp_query->found_posts || $this->get_pagenum() === 1 ) {
            $total_items = $wp_query->found_posts;
        } else {
            $post_counts = (array) wp_count_posts( $post_type, 'readable' );

            if ( isset( $_REQUEST['post_status'] ) && in_array( $_REQUEST['post_status']$avail_post_stati, true ) ) {
                $total_items = $post_counts[ $_REQUEST['post_status'] ];
            } elseif ( isset( $_REQUEST['show_sticky'] ) && $_REQUEST['show_sticky'] ) {
                $total_items = $this->sticky_posts_count;
            } elseif ( isset( $_GET['author'] ) && get_current_user_id() === (int) $_GET['author'] ) {
                $total_items = $this->user_posts_count;
            } else {
                $total_items = array_sum( $post_counts );

                // Subtract post types that are not included in the admin all list.

function wp_dashboard_right_now() {
    ?> <div class="main"> <ul> <?php     // Posts and Pages.     foreach ( array( 'post', 'page' ) as $post_type ) {
        $num_posts = wp_count_posts( $post_type );

        if ( $num_posts && $num_posts->publish ) {
            if ( 'post' === $post_type ) {
                /* translators: %s: Number of posts. */
                $text = _n( '%s Post', '%s Posts', $num_posts->publish );
            } else {
                /* translators: %s: Number of pages. */
                $text = _n( '%s Page', '%s Pages', $num_posts->publish );
            }

            $text             = sprintf( $textnumber_format_i18n( $num_posts->publish ) );
            


// Get all nav menus. $nav_menus  = wp_get_nav_menus();
$menu_count = count( $nav_menus );

// Are we on the add new screen? $add_new_screen = ( isset( $_GET['menu'] ) && 0 === (int) $_GET['menu'] ) ? true : false;

$locations_screen = ( isset( $_GET['action'] ) && 'locations' === $_GET['action'] ) ? true : false;

$page_count = wp_count_posts( 'page' );

/* * If we have one theme location, and zero menus, we take them right * into editing their first menu. */
if ( 1 === count( get_registered_nav_menus() ) && ! $add_new_screen
    && empty( $nav_menus ) && ! empty( $page_count->publish )
) {
    $one_theme_location_no_menus = true;
} else {
    $one_theme_location_no_menus = false;
}
Home | Imprint | This part of the site doesn't use cookies.