wp query – Previous / Next Links For Custom Post Type Sorted By Meta_Value

Question

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.

0
Kevin Burnett 1 month 2022-08-17T15:32:07-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse