If you prefer the $wpdb direct queries you can use something like this:
global $wpdb;
$count = $wpdb->get_var( "SELECT COUNT(ID) FROM {$wpdb->posts} WHERE post_type="code" and post_status="publish"" );
echo $count;
In the sql query above change the post_type
to whatever you like. This will return count of published posts under specific post type only.
If you want to query counts from multiple post types do this query:
global $wpdb;
$count = $wpdb->get_var( "SELECT COUNT(ID) FROM {$wpdb->posts} WHERE post_type IN ( 'code', 'shop', 'post', 'etc' ) and post_status="publish"" );
echo $count;
Hope that helps.
Related Posts:
- Inject post (from specific category) between posts in Loop
- Get taxonomy terms only of the WP_Query current posts
- Remove query string specific key value
- Hide post if matches current month and year
- How can I use wp_query to show all product data using just the products ID?
- How to store post ID’s in cookie or session to display the same posts later
- WP_Query: getting posts where custom field exists
- Array to string conversion on array_map
- Store metakey value as an array
- Increment paged on WP_Query
- Modify main query before it run without pre_get_post
- Do not duplicate posts with multiple categories in multiple loops
- array_rand not working correctly?
- WP_Query() load selected post
- Wp Query : Order by distance lat,lon
- WordPress sorting posts by date and title using a dropdown
- posts_per_page displays only 2 posts instead of 4 posts
- Query on a repeater date (acf)
- Need to show 7 posts from actual date
- Check the database for a postmeta field
- Getting info about selected posts using one WP_Query
- How to display last whole post on the homepage
- Get the total Author Favorited posts
- For each 3 posts, show a different post type
- Only Get A Certain Number of Posts From WP_Query
- insert thumbnail image from php script
- WP_Query count of different meta key values [duplicate]
- Weird Behaviour: Not all WordPress Posts appearing
- List of child pages fetch next results at link click
- Query category-specific, paginated posts and allow viewer to change sort order
- Why does WP_Query show only the same post even with different categories and endwhile?
- How to modify this function to exclude also the post belonging to a specific category?
- How to use a conditional statement in a post loop but not count towards the “posts_per_page” if false
- Count the number of matching post names in foreach loop
- How to make posts_like_dislike reference current post when iterating?
- Displaying a button on each post
- Trying to make php run in a post
- Get the last month with posts using a recursive function…
- Use WP_Query in shortcode
- Creating bulk posts with Youtube videos
- have_posts() execution failure
- Accessing post->ID outside of the loop for listing child pages
- Remove posts after a given amount of time
- Automatically add custom CSS to new posts using a category template
- How to filter posts by post format “standard” from wp-json api?
- Why is variable not working on custom sql query using wpdb?
- How to do multiple searches (with logical OR) in WP_Query in hook pre_get_posts?
- How Display Posts on category
- Hide wordpress field if data is empty in post!
- Can’t search posts using WP_QUERY inside AJAX Function
- How to query for pages/post depending on slug?
- Adding country tags automatically
- Accessing post’s meta data based on user’s click of a post
- Query all posts of a custom taxonomy term
- Get Current User Id Inside a Loop Returns 0 For a Shortcode
- Retrieving Author ID in wp-admin area
- Random order of WP_Query results with highest meta value
- Custom page archive query by url var calling page twice on one of my custom queries?
- WordPress custom slug (endpoint) and compare all links
- Getting a specific post values to another div or modal
- How to query for posts with a null or blank post_name?
- WordPress pagination not working with search page
- get current custom post ID by WP_Query method
- Trying to add attribute to my posts’ featured image
- How to securely provide a $_POST var in WP_Query with PHP 7?
- How to make sure content doesn’t display if selection is empty
- Displaying the last post on static homepage
- Using foreach loop breaks
- Can’t get wp_insert_post to work
- Why not yield/iterable in posts interface?
- Create WP_Query to search for posts by their categories or their parent/child categories
- WP Query with custom Shortcode
- Best practice for migration friendly images in posts/pages?
- wp_query with cat not working
- Advanced Meta Query for Large Calendar Website (12k+ posts) (175k+ wp_postmeta rows)
- How to use an associative array in post__in with WP_Query?
- How to show part of the_content?
- How do I “get the next 10 posts after post_id == x”?
- User’s total comment count excluding comments on their own posts
- If Query In Sidebar
- Recent Posts slider, using WP_Query(), loads duplicate sliders
- Why WP_Query(‘showposts=5’) shows only 1 post?
- Compare post-IDs within WP_Query? (Less than / Greater than)
- Unreadable pagination
- 200 Rewrite blog post links
- How to set a min number of words for a blog post
- How to use $query->set with post__in?
- Loop through categories and display posts title under each dropdown
- Get post id in a function when edit/add a post
- Why do WP_Query results change after updating unrelated Advanced Custom Fields (ACF)?
- 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
- Loading the same WP_Query in two different wordpress .php templates
- How to prevent WP_Query function from returning all posts when empty?
- wp_trim_words() does not work with my code Am I doing any mistake in my code?
- Dynamic content based on a URL parameter
- Display a specific category of products in shop page and disable code for specific actions
- WP_Query adds “(wp_posts.ID = ‘0’)” so no results are returned
- Second transition_post_status hook fired instead of the first
- Add more button if more 8 items