How to show posts rank based on custom field value

As you have stated, rank is defined by a value in a custom field, so you would just need to get the value from the custom field and display it.

You can try the following inside your loop

global $post;

// Just make sure the custom field name is correct
$rank = get_post_meta( $post->ID, 'custom_field', true ); 

// Display the title and rank
echo 'Rank ' . $rank . ' ' . get_the_title();

EDIT

Your ordering is incorrect in your code. Loop one should be sorted accoring to your custum field value. In loop 2, you would want to sort by the order of the ids passed to post_in

Your complete code should look like the following

$args = [
    'posts_per_page' => 100,
    'fields'         => 'ids',
    'meta_key'       => 'custom_field',
    'orderby'        => 'meta_value_num', //or 'meta_value_num'
    'order'          => 'DESC',
    // Add additional args here
];
$post_ids = get_posts( $args );

if ( $post_ids ) {
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
    $args_2 = [
        'paged' => $paged,
        'post__in' => $post_ids,
        'posts_per_page' => 10,
        'orderby'   => 'post__in',
    ];
    $q = new WP_Query( $args_2 );
    while ( $q->have_posts() ) {
        $q->the_post();

        global $post;

        // Just make sure the custom field name is correct
        $rank = get_post_meta( $post->ID, 'custom_field', true ); 

        // Display the title and rank
        echo 'Rank ' . $rank . ' ' . get_the_title();

        // Rest of your loop here
    }
        next_posts_link( 'Next Posts', $q->max_num_pages );
        previous_posts_link( 'Previous Posts' );
        wp_reset_postdata();
}

EDIT 2

To display the rank as number in query, you can do the following; (NOTE: This is untested)

function get_post_rank( $query = '' ) 
{
      global $wp_query;

    // If $query is empty or not an instanceof WP_Query, use $wp_query
    if ( !$query || !$query instanceof WP_Query) 
        $query = $wp_query;

    // Set up our variables for calculations
    $current_post = ( $query->current_post + 1 );
    $paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
    $ppp = $query->query_vars['posts_per_page'];

    // If this is the first page, simple return the current post number
    if ( $paged == 1 )
        return $current_post;

    // If current page is not 1, calculate our post number
    return ( $paged * $ppp ) - ( $ppp - $current_post );
}

Then inside the loop, do the following:

$args = [
    'posts_per_page' => 100,
    'fields'         => 'ids',
    'meta_key'       => 'custom_field',
    'orderby'        => 'meta_value_num', //or 'meta_value_num'
    'order'          => 'DESC',
    // Add additional args here
];
$post_ids = get_posts( $args );

if ( $post_ids ) {
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
    $args_2 = [
        'paged' => $paged,
        'post__in' => $post_ids,
        'posts_per_page' => 10,
        'orderby'   => 'post__in',
    ];
    $q = new WP_Query( $args_2 );
    while ( $q->have_posts() ) {
        $q->the_post();

        $rank = get_post_rank( $q ); 

        // Display the title and rank
        echo 'Rank ' . $rank . ' ' . get_the_title();

        // Rest of your loop here
    }
        next_posts_link( 'Next Posts', $q->max_num_pages );
        previous_posts_link( 'Previous Posts' );
        wp_reset_postdata();
}

Leave a Comment