Skip to content
Read For Learn
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP

Display a post count from a custom metabox selection

Try this code:

function postsCount($meta_value) {
    $args = array(
        'numberposts'   => -1,
        'post_type'        => 'custom_post_type', // set you custom post type
        'meta_key'         => 'project_select',
        'meta_value'       => $meta_value,
    );

    $my_posts = get_posts( $args ); 
    $postsCount = count($my_posts);
    return $postsCount;
}
if ( is_user_logged_in() ) {
    echo postsCount('Draft');
    echo postsCount('Completed');
}

or if you want to get all counts by less code, try this:

function postsCount() {
    if ( !is_user_logged_in() ) {
        return;
    }
    $meta_value_array = array( 'Draft', 'Open', 'Pending', 'Closed', 'Completed');
    $postsCount="<ul>";
    foreach ($meta_value_array as $meta_value) {
        $args = array(
            'numberposts'   => -1,
            'post_type'        => 'custom_post_type', // set you custom post type
            'meta_key'         => 'project_select',
            'meta_value'       => $meta_value,
        );

        $my_posts = get_posts( $args ); 
        $postsCount = count($my_posts);
        $postsCount .= '<li>' .$meta_value. ' ' .$postsCount. '</li>';
    }
    $postsCount .= '</ul>';
    return $postsCount;
}

echo postsCount();

Related Posts:

  1. Meta box with front-end styling
  2. Edit post meta direct from post.php?
  3. How to force one column layout on custom post type edit page?
  4. How can I add a column/s to wp_posts table?
  5. How do I remove all the metaboxes for a custom post type?
  6. WP_List_Table Inside Metabox Not Working on Submit
  7. Add metabox with media uploader in a custom post type [duplicate]
  8. Get all posts from custom post type and insert in select input as metabox
  9. Detect meta value changes when post is updated (post_updated)
  10. Creating a custom post type upon registration
  11. Having trouble with custom post type / meta box
  12. add_meta_box: Datepicker like the one for postdate?
  13. How can I include meta box content when searching?
  14. update a post meta from a single table cell TablePress
  15. update custom post type meta from a shortcode
  16. Custom Meta Boxes – Nonce Issue – Move to trash issue
  17. Displaying Meta Box Image
  18. Change Post Title Edit Box
  19. Multiple information using custom post type
  20. How to keep a check box in custom meta box for custom post type checked by default for add new post?
  21. How to add multiple featured image in meta box in post editor?
  22. Show metabox in custom-post-template depending on taxonomy term?
  23. Formatting custom meta box date from YYYY/MM/DD to a more readable alternative
  24. Displaying custom posts only if custom meta box’s date is not expired
  25. Displaying Metabox value (custom post type taxonomy)
  26. How can I display my custom metaboxes on a custom post template?
  27. Meta Box Only in CPT Edit Page, Not in Add New
  28. get_post_meta is returning image id
  29. WordPress Orderby Numeric Value Not Working
  30. Populate Custom Fields in a Custom Post Type?
  31. Adding dropdown select meta box to custom post type – seems restAPI is interfering
  32. Custom meta box data array: foreach not working correctly?
  33. Undefined Variable – Custom Post Type Meta
  34. Show the same Article Available in Other Categories
  35. how to delete all users and posts based on ‘user_meta’?
  36. Populate dropdown from one custom post type inside another custom post type
  37. How to check if user meta field is empty in conditional else statement
  38. Post image in WordPress not appearing on home page
  39. Changing custom type name hides the posts
  40. Update Post Meta for a logged in user
  41. A better way to add a meta box to custom post types
  42. Restrict Access to Posts based on Custom User and Post Meta Data
  43. Two Custom Post Types Many to Many Relationship
  44. Update postmeta Parent when post_status child change
  45. Metabox not show in categories custom post type cmb2
  46. Show Custom Post Type meta boxes only on Page Edit
  47. Can’t publish custom post type – “You are not allowed to edit this post.”
  48. Meta Data for Custom Post Type not saving
  49. create custom meta box with default value
  50. Displaying a div from an assigned meta_value when on a page
  51. How to Echo Metadata Value in Currency Format
  52. Issue on Working with Metabox – Checkbox to enable Sale Items
  53. Why do I have to press the “Update” button twice to save my meta box values?
  54. Values from meta-box not on $_POST
  55. Custom post type suddenly stoped working after WordPress 5 update
  56. Batch Extract Date from post title and put into ACF custom field
  57. How can I output WPAlchemy repeating fields meta values in my page template?
  58. Meta box data is saved but NOT displayed in the meta box text field. Why?
  59. Save Metabox Custom Field Value
  60. Meta box data not saving
  61. WP_Meta_Query object with conditionals
  62. Related posts meta_query CPT
  63. Sanitaizing Select Optin For Custom Post Type Metabox in WP
  64. How to retrive Custom Post Type Meta Fields in Custom WP_Query
  65. Cannot Save MetaBox Data in Custom Post Type
  66. Add a class to post_class if more than one post shares same meta_value_num
  67. Function to return values from metabox
  68. Add a meta field to the list of results for a custom post type
  69. Empty meta-box returns publishdate if no value is set?
  70. Custom Meta Box with variable number of fields
  71. Create a custom post type based on ‘Post’
  72. Custom Post Type meta data getting deleted on bulk editing taxonomies
  73. Get_post_custom not fetching value from array wordpress
  74. Add a form in every post and save data in post meta
  75. SELECT custom post type and its meta in SQL
  76. Save data is post using php front end
  77. Creating a custom post type upon registration for a specific user role
  78. Show posts from WP Custom Post Type selected from a field in a metabox
  79. $_POST from a Meta Box
  80. Get meta values from parent post and save in child post
  81. How to properly get the wp_postmeta.meta_value of a custom post type in specifics?
  82. Metaboxes in Loop
  83. Custom meta box data not saving
  84. update a posts of other custom post type
  85. Collect custom post in a calendar
  86. Delete custom post type metadata without deleting the post in admin area
  87. Problem with ‘save_post’ hook not running
  88. How to make post page unique and show it on home page?
  89. How to Arrange Support => Thumbnail
  90. Howto: use existing post_meta as options for a different metabox (checkboxes or list)
  91. How to create content automatically when a post is published?
  92. Top 30 Songs using Custom Post Type
  93. Post AND page parameter for WP function
  94. How to display Author Profile based on Custom field value
  95. Use a Variable in update_post_meta as the $meta_key
  96. Automatically add custom taxonomy when meta value gets to a set number or beyond
  97. Canot save post if any meta box was empty
  98. I am having a problem with fetching product data in the Gutenberg block editor
  99. Open Custom Post Type as PDF
  100. Update CPT post meta with update_post_meta and cron job
Categories custom-post-types Tags custom-post-types, metabox, post-meta, posts, user-meta
Grab next post title in archive page
If on Category Page add “active” class?

Recommended Hostings

Cloudways: Realize Your Website's Potential With Flexible & Affordable Hosting. 24/7/365 Support, Managed Security, Automated Backups, and 24/7 Real-time Monitoring.

FastComet: Fast SSD Hosting, Free Migration, Hack-Free Security, 24/7 Super Fast Support, 45 Day Money Back Guarantee.

Recent Added Topics

  • Bug in translation system: load_theme_textdomain() returns true, files are available and accessible but the language defaults to english
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • Get the name of the template/*html file used
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
  • How to display the description of a custom post type in the dashboard?
  • Critical error on image display
  • Copying WP data and files into new install?
  • How to determine the DirectAdmin WordPress backup date?
  • How to get list of ALL tables in the database?
© 2026 Read For Learn
  • Database
    • Oracle
    • SQL
  • algorithm
  • asp.net
  • assembly
  • binary
  • c#
  • Git
  • hex
  • HTML
  • iOS
  • language angnostic
  • math
  • matlab
  • Tips & Trick
  • Tools
  • windows
  • C
  • C++
  • Java
  • javascript
  • Python
  • R
  • Java Script
  • jQuery
  • PHP
  • WordPress