You could use a Twig filter to query a custom post number index which is saved in a transient: the order is currently set to the menu order and transient is cached for 1 minute.
Twig
{{ post.id|get_post_number }}
php
/**
* Adds functionality to Twig.
*
* @param \Twig\Environment $twig The Twig environment.
* @return \Twig\Environment
*/
add_filter( 'timber/twig', 'add_to_twig' );
function add_to_twig( $twig ) {
// Adding functions as filters.
$twig->addFilter( new Timber\Twig_Filter( 'get_post_number', 'get_post_number_by_id' ) );
return $twig;
}
/**
* Get post number from ID, based on menu order.
*
* @param [type] $id
* @return void
*/
function get_post_number_by_id( $id ) {
// Check we have a string
$target_id = strval( $id );
// Save quesry and index in transient
$post_number_index = Timber\Helper::transient(
'get_post_number_by_id',
function () {
// Update parameters to match desired search order
$args = array(
'orderby' => 'menu_order',
'posts_per_page' => -1,
'post_status' => 'publish',
);
// Query
$posts = get_posts( $args );
if ( !empty( $posts ) ) {
// Store list
$data = [];
// Set post numer count
$count = 0;
// build index.
foreach( $posts as $post ) {
$data[ $post->ID ] = array(
'post_id' => $post->ID,
'post_number' => ++$count,
);
}
return !empty( $data ) ? $data : false;
}
return false;
},
MINUTE_IN_SECONDS
);
// Retrun post number
if ( !empty ( $post_number_index ) && array_key_exists( $target_id, $post_number_index ) ) {
return $post_number_index[ $target_id ]['post_number'];
}
return false;
}
ol
or a css counter? – DarkBee