1

I use a custom field with is casino_rank_scorewith stores a point rating for example 4.9

All casinos is stored on custom post type casinos.

I need to figure out how to get a rank listing like RANKED #2 of 350 CASINOS from points.

Wordpress stores this data to wp_postmeta table.

I can get all points from custom post type, from all casinos and put into array, but what i need to do next with custom php to make rank sorting.

I read this but there is different structures and data.

2

You can run a WP_Query to get all the posts from post_type casinos and sort them with the custom field value casino_rank_score. Then you can further process it to display ranks.

$args = array(
    'post_type' => 'casinos',
    'meta_key' => 'casino_rank_score',
    'orderby' => 'meta_value_num',
    'posts_per_page' => 350,
    'ignore_sticky_posts' => 1,
);

$my_query = new WP_Query( $args );

If you only need to display one post only like RANKED #2 of 350 CASINOS then you can limit your results with 'posts_per_page' => 1, and offset by 1 'offset' => 1, This wway you will get 2nd ranked casino.

EDIT

To get rank you will need to run a custom query. And this is how you can get rank of current post in your custom query. It is optimized because we are only returning ID, no post data is requested so it's pretty fast. But to optimize it even further, you can cache this query with Transients_API for couple of hours or days.

$args = array(
    'post_type' => 'casinos',
    'meta_key' => 'casino_rank_score',
    'orderby' => 'meta_value_num',
    'posts_per_page' => -1,
    'ignore_sticky_posts' => 1,
    'fields' => 'ids',
);

$my_query = new WP_Query( $args );

global $post;

$total_casinos = $my_query->post_count;
$casinos_rank = array_search( $post->ID, $my_query->posts ) + 1;

echo "RANKED #" . $casinos_rank . " of " . $total_casinos . " CASINOS";

This is tested and working

  • But how i get the text ranked #2 that will be getting dynamicaly from scrores data. – Foxsk8 Sep 30 '14 at 18:34
  • You can use a variable to count items and output counter value. Ok first tell me you knly want to return 1 item or list of items. – Robert hue Sep 30 '14 at 18:38
  • I need this data to show only on single casino template not make a rank listing. – Foxsk8 Sep 30 '14 at 18:39
  • Oh, that complicates the issue. Although we can always run a custom query on single page to get rank of the post but I will see it there is a simpler solution. Interesting question. – Robert hue Sep 30 '14 at 18:45
  • Yes. Interesting question. Also need looking that query and all staff is not so slow to make rank queries. Maybe this staff need make with php functions, get all data into array and then make a manupilations for that, not make a WP_Query? – Foxsk8 Sep 30 '14 at 18:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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