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 current ranking of post as a number in post title

Could make it work already 🙂

     class MY_Post_Numbers {

    private $count = 0;
    private $posts = array();

    public function display_count() {
        $this->init(); // prevent unnecessary queries
        $id = get_the_ID();
        echo sprintf( '<div class="post-counter">Post number<span class="num">%s</span><span class="slash">/</span><span class="total">%s</span></div>', $this->posts[$id], $this->count );
    }

    private function init() {
        if ( $this->count )
            return;
        global $wpdb;       
        $posts = $wpdb->get_col( "SELECT $wpdb->posts.ID
                FROM $wpdb->posts
                left join $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id)
                WHERE $wpdb->posts.post_status="publish"
                AND $wpdb->posts.post_type="hp_listing"
                AND $wpdb->postmeta.meta_key = 'hp_rating'
                ORDER BY ($wpdb->postmeta.meta_value + 0) DESC");
        $this->count = count($posts);

        foreach ( $posts as $key => $value ) {
            $this->posts[$value] = $key + 1;
        }
        unset($posts);
    }

}
$GLOBALS['my_post_numbers'] = new MY_Post_Numbers;

function my_post_number() {
    $GLOBALS['my_post_numbers']->display_count();
}

with

<?php my_post_number(); ?>

it’s possible to use in template file if anyone else is interested

Related Posts:

  1. How to get all term meta for a taxonomy – getting term_meta for taxonomy
  2. Short of raw SQL, can I query for multiple attachment metadata that have a given array key?
  3. Triple meta_key on custom SELECT query
  4. Get updated post meta on save_post action?
  5. Job of meta_key meta_value fields in database tables
  6. order by multiple meta_keys?
  7. How to get the total number of meta_values based on a custom post type?
  8. WP_POSTMETA – What do these values mean inside the data structure?
  9. MySQL Query To Select Post By Postmeta
  10. How can I convert postmeta from unserialized to serialized?
  11. Set Condition echo function get_post_meta
  12. Get a row from a separate table by matching a posts meta_key to a tables ID column
  13. Compare string with post id in wpdb and do stuff when match is found
  14. Read post meta values, only if posts are public
  15. How to test the outcome of a wpdb query?
  16. why after saving meta value it’s saving all the values the one that i clicked?
  17. Is it safe to add a new field to meta_value field?
  18. How can I use ‘orderby’ => ‘meta_value_num’ to order by the numerical value even if the value starts with a word?
  19. wpdb->get_var – count author posts, meta value
  20. How to query and update one colum in postmeta table?
  21. I can not display meta value in extras.php and template-tags.php
  22. get specific values from WordPress meta_value
  23. Count Post and Page Views based on meta_value Using Shortcode in Dashboard Widget
  24. Negative meta_query if storing multiple post_meta values with shared meta_key
  25. if get_post_meta function returns empty – Do Not Display HTML
  26. Get data from custom table and update relative post_meta based on meta_key
  27. How to get the total of two meta values from different meta keys?
  28. How to save a meta_value as a numeric value after I retrieve it via update_post_meta?
  29. How to update/add child posts meta whenever the parent post meta is updated?
  30. Get table parameter and save in meta value
  31. Multiple meta key and value search in the query
  32. Exclude category from DB query
  33. How to sort by meta value num, but ignore zero value?
  34. Fetching array of postmeta with $wpdb and in_array conditional
  35. Which query method to use? (edit- wpdb syntax problems)
  36. Cache metadata for set of posts
  37. How to get all the related ids without array?
  38. Update post meta array – add new, single value
  39. How do I retrieve the slug of the current page?
  40. Most efficient way to get posts with postmeta
  41. How to only display posts whose meta_value field is not empty?
  42. Get posts by meta value
  43. Explanation of update_post_(meta/term)_cache
  44. How to extract data from a post meta serialized array?
  45. Query to sort a list by meta key first (if it exists), and show remaining posts without meta key ordered by title
  46. How to save an array with one metakey in postmeta?
  47. WordPress is stripping escape backslashes from JSON strings in post_meta
  48. How can I get the post ID from a WP_Query loop?
  49. Check if Post Title exists, Insert post if doesn’t, Add Incremental # to Meta if does
  50. How to update_post_meta value as array
  51. Adding meta tag without plugin
  52. What’s the point of get_post_meta’s $single param?
  53. What is the different between an attachment in wp_posts and an attachment in wp_postmeta?
  54. How to edit a post meta data in a Gutenberg Block?
  55. Sanitizing integer input for update_post_meta
  56. post formats – how to switch meta boxes when changing format?
  57. Execute action after post is saved with all related post_meta records (data)
  58. Lack of composite indexes for meta tables
  59. Get a single post by a unique meta value
  60. SELECT max(meta_value) FROM wp_postmeta WHERE meta_key=’price’… stops working when value is over 999
  61. if get_post_meta is empty do something
  62. How we get_post_meta without post id
  63. How get post id from meta value
  64. What is the code to get the download link for a product in WooCommerce?
  65. Safe to delete blank postmeta?
  66. So much data in postmeta
  67. Dealing with Many Meta Values, 30+
  68. advanced custom fields update_field for field type: Taxonomy
  69. update_post_meta not saving when value is zero
  70. Content hooks vs User hooks
  71. Meta compare with date (stored as string) not working
  72. Trying to get custom post meta through Jetpack JSON API [closed]
  73. How to update/insert custom field(post meta) data with wordpress REST API?
  74. What is the advantage of the wp_options design pattern?
  75. Restrict post edit/delete based on user ID and custom field
  76. get_post_meta returning empty string when data shows in the database
  77. publish_post action hook doesn’t give post_meta_data
  78. Remove WordPress.org Meta link
  79. Remove post meta keys
  80. How to access the post meta of a post that has just been published?
  81. Why time functions show invalid time zone when using ‘c’ time format?
  82. Why is get_post_meta returning an array when I specify it as single?
  83. How to update/delete array in post meta value?
  84. Adding an assisting editor box to Post page
  85. WPDB Multiple site’s posts and get featured images
  86. delete unused postmeta
  87. Should I sanitize custom post meta if it is going to be escaped later?
  88. Add post meta based on another post meta value before publish post
  89. How do I retrieve multi-dimensional arrays from the wp_postmeta table, & display on a website?
  90. Front-end update_post_meta snippet displays white screen?
  91. Query between two meta values?
  92. Save both current and new version of post meta
  93. Get Advanced Custom Fields values before saving [closed]
  94. Give extra post-meta to RSS feeds
  95. How to get meta value in wp_attachment_metadata
  96. WP REST API “rest_no_route” when trying to update meta
  97. Clean up output added via wp_head()
  98. List posts under meta_value heading
  99. WordPress Admin Panel search posts with custom post meta values along with title
  100. Why am I getting an infinite loop with have_posts?
Categories post-meta Tags meta-value, post-meta, wpdb
Multiple domain names map to one site of wordpress subdomain multisite
WordPress redirects page query parameter in URL

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