0

I have total 100 posts, 10 posts in each page on homepage with paginatin. Here is my code

$args = [ 'posts_per_page' => 100, 'fields' => 'ids', // 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, 'meta_key' => 'custom_field', 'orderby' => 'meta_value_num', //or 'meta_value_num' 'order' => 'DESC', ]; $q = new WP_Query( $args_2 ); while ( $q->have_posts() ) { $q->the_post();?> //loop here <?php } next_posts_link( 'Next Posts', $q->max_num_pages ); previous_posts_link( 'Previous Posts' ); wp_reset_postdata(); ?> 

I want to show Rank # before title based on custom field value.

Example:

Rank # Post Title

3
  • 1
    How do you define rank? Commented Jul 16, 2015 at 17:31
  • I define it based on custom field value. Commented Jul 16, 2015 at 17:32
  • 1
    OK, great, I'm posting an answer Commented Jul 16, 2015 at 17:35

1 Answer 1

1

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(); } 
15
  • Pieter Goosen I add random numbers in million and billions using custom field and i want to show rank based on the highest number. Commented Jul 16, 2015 at 18:04
  • Not getting you. Does my code not display. The code in your question should do that ordering already Commented Jul 16, 2015 at 18:06
  • See my update, this should work now Commented Jul 16, 2015 at 18:24
  • It simply show the numbers entered in custom fields. I want to show rank based on numbered entered in custom field. Post one - custom field value = 400 Post two - custom field value = 500 Post three - custom field value = 100 now rank should be like below Rank#1 Post two Rank #2 Post one Rank #3 Post three and so on. hope I am not confusing. Commented Jul 16, 2015 at 18:49
  • So post 1 should be #1 and the last post #100 Commented Jul 16, 2015 at 18:53

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.