In order to use wp_count_posts()
as you would on a single site, pass the blog id into the switch_to_blog()
function before calling wp_count_posts()
.
Example:
function wpse_get_post_count( $blog_id ){
switch_to_blog( $blog_id );
$post_count = wp_count_posts();
restore_current_blog();
return $post_count;
}
Ensure restore_current_blog()
is used after every switch_to_blog()
or you will risk difficulties mentioned in the notes section of the documentation and this WordPress Stack Exchange answer.
Related Posts:
- How to avoid duplicates when creating recent network posts
- Find Site ID From WP_Post
- How Display Posts on category
- Hide wordpress field if data is empty in post!
- How can I use wp_query to show all product data using just the products ID?
- Query all posts of a custom taxonomy term
- Retrieving Author ID in wp-admin area
- How to add values to media “whitelist_options” in wp-admin/options.php template?
- Getting a specific post values to another div or modal
- Trying to add attribute to my posts’ featured image
- Displaying the last post on static homepage
- Can’t get wp_insert_post to work
- Why not yield/iterable in posts interface?
- Best practice for migration friendly images in posts/pages?
- How to change home or site url using action hooks or filter?
- How to show part of the_content?
- Unreadable pagination
- 200 Rewrite blog post links
- How to set a min number of words for a blog post
- wordpress media library new uploads don’t write to database
- Loop through categories and display posts title under each dropdown
- Get post id in a function when edit/add a post
- how do I get a specific post from a post with a subcategory in WP
- Automatically create child pages and grandchild pages when saving a (parent) page
- wp_trim_words() does not work with my code Am I doing any mistake in my code?
- Second transition_post_status hook fired instead of the first
- WordPress returning 404 for multisite pages
- How to call plugin function per site in a multisite?
- How to store post ID’s in cookie or session to display the same posts later
- WordPress returns “The link you followed has expired” error page whenever I add a new site, add a user etc
- Which hook/action will help me solve my problem?
- How can I add extra word in permalink when someone click download button?
- Post + form + action + results on the same page
- Open post-content in archive page in a Modal box with bootstrap
- NGINX rewrite rules for multisite
- Make a list of sites for each user in WPMU – switch_to_blog (display in SITE_ID: 1)
- remove post that has no content
- Loading index.php contents which located outside blog folder for post single page
- get post content of particular post by url
- Post repeating with infinite scroll
- Custom posts in different columns style
- Get children post mime type using parent post_ID wp_post
- How to show only subcategories in parent category not parent category?
- WP & Server Speed [Teacher Question]
- 404 error when I acess the second page of post-type
- Redirect to another page using contact form 7? [closed]
- Which PHP page is the Default Posts Page
- Moving code from theme header to functions.php
- retrieve post data and insert it in seperate static html page without WordPress
- WP Insert Post and then go to post
- Why am I getting a different filename? And how does WordPress load singular.php for both Page & Post? (Fresh WordPress installation)
- Returning data instead of echoing/printing
- Display related posts without a plugin
- Counting Search results, and displaying the offset per page
- Is it best to avoid using $wpdb for security issues?
- Function works everywhere, how to keep it to execute when creating a new post
- Making (and edit existing) posts with ID included
- Copy taxonomy terms from one post to another programmatically
- wp_redirect only works on main site and not on other sites
- Parsing Menu Items and Blog Posts
- Pagination on Custom Post
- $wpdb returns duplicate posts
- How to show an entire post content and not also the excerpt?
- Get latest post from all categories except one
- What file have I to create in my custom WordPress theme to show all the post belonging to a specific category?
- Update post title from external file?
- Parse error: syntax error, unexpected end of file
- How do I display offsite database info on my wordpress site?
- Timthumb.php image gallery not working on Multisite WordPress
- Automatically add images to a menu
- Why is a wp function used in current PHP namespace’s callback not resolved to global scope?
- Load Posts on Click via Ajax into a DIV
- How to catch wordpress post ID when it’s published
- set post limit at load more ajax wordpress
- WordPress query undefined offset in loop
- WP_Query: getting posts where custom field exists
- List all blogs, but exclude the main site
- Array to string conversion on array_map
- Store metakey value as an array
- Increment paged on WP_Query
- Only show first image in foreach loop
- How to disable all logins except Network login in WordPress Multi site?
- How to use mysql LIKE with wpdb?
- Reading settings in the home page precisely home.php
- Do I need to edit my theme in order to change the title of my blog page?
- Add new post in existing categories using wp_insert_post
- WordPress upload file size error even after raising php limits
- How do I add more options to the post-new.php page?
- get post id from wp_insert_post for get_template_part
- create front-end users post list by specific category
- Incrementing PHP variable onclick to display posts by month
- Modify main query before it run without pre_get_post
- Displaying list of posts in category page
- Known Issues in WordPress When Upgrading PHP to ver 7
- .html end of URl affected to SEO?
- How can I get “Previous” and “Next” to show in the navigation besides the links?
- WordPress homepage setup not working on first load
- Get the id of all images in a post
- Displaying content on posts page based on category
- Do not duplicate posts with multiple categories in multiple loops