0

I have a page where people can rank stuff. My posts are ordered by the current ranking which is stored as a meta key in post meta.

Now i want to add the current ranking spot within the title of the single post page.

I found this code which is working fine with adding the ranking number according to post date:

    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 ID FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post' ORDER BY post_date " ); // can add or change order if you want 
        $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();
}

Now i only need to make it work with my meta key value instead of order by post_date

$posts = $wpdb->get_col( "SELECT ID FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post' ORDER BY post_date " );

Can someone help me changing this line so i can order by my ranking meta value?

The meta value is a decimal number like e.g. 3.745 .

Kinda lost right now already tried a lot with this but nothing worked yet

4
  • What is your meta key?
    – admcfajn
    Jul 9, 2022 at 6:01
  • its called "hp_rating"
    – Jawequo
    Jul 9, 2022 at 10:46
  • How the rankings are stored in meta?
    – Abhik
    Jul 9, 2022 at 11:43
  • the rankings are stored for each custom post called "hp_listing" under the meta_key "hp_rating" and a meta_value from 1-5 but with up to 3 decimals like e.g. 4.755. Everytime a user rates the meta_value gets updated. I want to number the current ranking DESC and ordered by the meta value so i can use display count from given code to show the current ranking number within the title on the single post page. Like: #5 POSTTITLE
    – Jawequo
    Jul 9, 2022 at 12:59

1 Answer 1

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.