简体   繁体   中英

Get index of post

Is it possible to get the index/item key of a post, based on the order I've inserted them in the admin backend?

eg there are a couple of Posts, 1-100, in the backend and I need a number that I can render, so the user can see which is the oldest and newest page.

You can do this using a custom field. Create a custom field ( for example with the name "post_number") and then you can sort the site posts ( and get the index ) using the following code:

$posts = get_posts([
  'post_type'        => 'post',
  'post_status'      => 'publish',
  'posts_per_page'   => -1, // Show all posts
  'orderby'          => 'meta_value_num',
  'order'            => 'DESC',
  'meta_key'         => 'post_number',
]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM