Previous / Next Links For Custom Post Type Sorted By Meta_Value

I was hoping for a nice and clean solution already existing within WordPress, but I didn’t have the time for someone to give it to me, so I created my own pagination routine I put into the functions.php of the theme and call it from the single-auction.php template…

function my_pagination($currentID){
    $args = array(
        'post_type' => 'auction',
        'meta_key' => 'date_time',
        'orderby' => 'meta_value',
        'meta_value' => date("Y-m-d h:i"),
        'meta_compare' => '>=',
        'order' => 'ASC',
        'posts_per_page' => -1,
    );

    $my_query = new WP_Query($args);
    
    //echo "<pre>"; print_r($my_query->posts); echo "</pre>";
    ?>
<nav id="post-navigation" class="navigation post-navigation" role="navigation" aria-label="Post Navigation">
    <div class="nav-links">
<?php get_previous_auction_link($my_query,$currentID); ?>
<?php get_next_auction_link($my_query,$currentID); ?>
    </div>
</nav>
<?php   
    wp_reset_query();
}

function get_previous_auction_link($query, $currentID) {
    for ($a = 0; $a < count($query->posts); $a++) {
        if ($a > 0 && $query->posts[$a]->ID == $currentID) {
?>
    <div class="nav-previous">
        <a href="<?php the_permalink($query->posts[($a - 1)]->ID); ?>" rel="prev">
            <span class="meta-nav" aria-hidden="true">Previous:</span> <span class="screen-reader-text">Previous auction:</span> <span class="post-title"><?php echo get_the_title($query->posts[($a - 1)]->ID); ?></span>
        </a>
    </div>
<?php
        }
    }
}

function get_next_auction_link($query, $currentID) {
    for ($a = 0; $a < count($query->posts); $a++) {
        if ($a < (count($query->posts)-1) && $query->posts[$a]->ID == $currentID) {
?>
    <div class="nav-next">
        <a href="<?php the_permalink($query->posts[($a + 1)]->ID); ?>" rel="next">
            <span class="meta-nav" aria-hidden="true">Next:</span> <span class="screen-reader-text">Next auction:</span> <span class="post-title"><?php echo get_the_title($query->posts[($a + 1)]->ID); ?></span>
        </a>
    </div>
<?php    
        }
    }
}

deneme bonusu veren sitelerbahis casinomakrobetceltabetpinbahispolobetpolobet girişpinbahis girişmakrobet girişpulibet girişmobilbahis girişkolaybet giriş