0

I created a template page for a custom post type called auctions-upcoming.php...

<?php
/**
 * Template Name: Upcoming Auctions
 */
get_header();

$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);
$options = get_option('auction_options');
?>
<!-- auctions-upcoming.php -->
<div class="container-fluid">
    <div class="row">
    <?php /* Start the Loop */ 
    while ($my_query->have_posts()) : $my_query->the_post(); $metadata = get_post_custom(get_the_ID()); ?>
        <div class="col-12 col-sm-12 col-md-12 col-lg-6 col-xl-4 mb-4">
            <div class="shadow">
                <div id="post-<?php the_ID(); ?>" <?php post_class('single-post m-0 p-0'); ?>>
                    <div class="auction-card">
                        <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(array(400,225), array('alt' => get_the_title(), 'class' => 'img-responsive h-auto mx-auto d-block')); ?>
                            <div class="overlay-text">
                                <h5><?php the_title(); ?></h5>
                            </div>
                        </a>
                    </div>
                    <div class="p-2">
                        <p>
                            <font color="#04B404">
                                <b>Auction Information:</b>
                            </font>
                        <br />
                        Auction Date: <b><?php echo date_format(date_create($metadata["date_time"][0]), "l, F j, g:i A"); ?></b><?php
                            if (!is_null($metadata["date_time_update"][0])) {
                                echo "<br />Last Updated: <b>" . date_format(date_create($metadata["date_time_update"][0]), "F j, g:i A") . "</b>";
                            }
                            ?>
                        </p>
                        <?php echo $metadata["location"][0] ? "<p><font color=\"#04B404\"><b>Auction Site:</b></font><br />" . nl2br($metadata["location"][0]) . "</p>" : ""; 
                        the_excerpt(); ?>
                    </div>
                </div>
            </div>
        </div>
<?php endwhile; ?>
    </div>
<?php //get_sidebar();  ?>
</div>

<?php
wp_reset_query();

get_footer();

The criteria for the query returns the custom post type auctions, it's sorted in ascending order by the meta_value of the meta_key called date_time, and where the date_time value is greater than the current year-month-day hour:minute.

Each auction returned uses a single page template called single-auction.php to display the details of each auction...

<?php
/**
 * The template for displaying single auction
 */
get_header(); 

?><!-- single-auction.php -->
    <div id="primary" class="content-area row">
        <div class="col-2"></div>
            <main id="main" class="site-main col-8" role="main">
            <?php
            // Start the loop.
            while ( have_posts() ) :
                the_post();

                // get content
                get_template_part( 'content', 'auction' );

                // Previous/next post navigation.
                the_post_navigation(
                    array(
                        'next_text'     => '<span class="meta-nav" aria-hidden="true">' . __( 'Next', 'bpa' ) . '</span> ' . '<span class="screen-reader-text">' . __( 'Next post:', 'bpa' ) . '</span> ' . '<span class="post-title">%title</span>',
                        'prev_text'     => '<span class="meta-nav" aria-hidden="true">' . __( 'Previous', 'bpa' ) . '</span> ' . '<span class="screen-reader-text">' . __( 'Previous post:', 'bpa' ) . '</span> ' . '<span class="post-title">%title</span>',
                    )
                );
                // End the loop.
            endwhile;
            ?>
            </main>
        <div class="col-2"></div>
    </div>
<?php get_footer();

My problem is the_post_navigation function returns the previous or next post in the order it was entered into Wordpress and not in the order of the meta_value of the date_time meta_key.

How do I make the_post_navigation function in the single-auction.php template follow the order established in the WP_Query on the auctions-upcoming.php template?

Indications are I need to use the get_{$adjacent}_post_sort hook, but I'm still not certain how to implement it.

1 Answer 1

0

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    
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.