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
- Best Practice for PHP
- Display user’s total comment count outside The Loop
- how to display active, upcoming and past event with featured listing with pagination
- Get list of WP Updates Across Sites
- New Plugin Review
- Use template for posts with a particular category grandparent
- Help with adding pagination to custom wp_query
- Styling images coming from another blog
- Exclude posts from homepage having a specified tag
- API response to be stored locally
- How to filter posts by post format “standard” from wp-json api?
- Retrieving Author ID in wp-admin area
- Can’t get wp_insert_post to work
- Why not yield/iterable in posts interface?
- Best practice for migration friendly images in posts/pages?
- 200 Rewrite blog post links
- 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?
- Second transition_post_status hook fired instead of the first
- Security for data obtained from the database
- How to overwrite orderby with add_query_var
- Get latest post from all categories except one
- Parse error: syntax error, unexpected end of file
- get_the_ID() in the footer returns wrong value
- In WordPress, how do I get the number of posts next to single_cat_title(”); in the category.php file?
- WP_Query multiple post results
- create front-end users post list by specific category
- Error display post thumbnails for previous and next post
- echo var into wp_query
- form $_post action value gets truncated after it passes through two forms
- Toolbar Hidden in a Virtual Page
- get value from get_post_meta then reuse it in another get_post_meta
- get different meta-data of a complicated query at the same time
- Build A Custom SQL Query for WordPress Search
- WordPress Custom Form – Getting Query Vars, Weird Glitch?
- Using a new WP_Query inside the loop
- How to Send Pingbacks for all Posts in WordPress?
- How to get specific multiple pages excerpts at homepage?
- Trying to get post ID outside loop on blog page
- Enqueue script only on child pages of custom post types
- Attach and retrive multiple pdf files to post or page
- Warning: count(): syntax problem of the count () function
- Wp-query output correct, but the loop shows one less item (only sometimes)
- Get list of posts from attachment
- How to display sticky post always at the top (before regular post) in wordpress?
- Counting Posts by Category
- How to get the posts that my following users are liked?
- Displaying POST content with HTML tags and all
- Pagination in WP Queries
- Add div after every 4 posts then every 2 posts for a responsive loop
- direct query to post_meta table
- Can’t put a hyperlink on Featured Post’s Image
- WordPress get_post_meta issue
- List sibling pages widget, exclude current page
- Display pages from specific page template
- Does wp_query and query_posts affect website performance? [duplicate]
- WordPress post pagination on custom template not working
- Ajax Load More or View More functionality for woocommerce category layout by template overriding
- How to display the date under the post title?
- WooCommerce: write featured image dimensions to custom fields in product’
- a problem in class in class-wp-hook.php
- Count custom posts type and filter by tag
- transition_post_status hook, works – but not if the post is new
- Exclude first post from wp query
- Warning: Undefined variable $post_id