Support

Account

Home Forums General Issues How to get the previous and next post ID when posts are sorted by custom field?

Unread

How to get the previous and next post ID when posts are sorted by custom field?

  • It is sort of a follow up question to the following issue. Basically a page where a date time field was attached to the post post type and then posts got sorted by that field with the constraint that only posts aka events today or in the future should be shown. In the other issue I’ve asked and afterwards described how I solved modifying the custom loop the Phlox theme the page is using by pre_get_posts. But the real nasty problems turned up when I noticed the previous and next links in the single post view. They were totally out of sort as well as showing also past events. I’ve learned then that the previous and next buttons utilize their own queries and by default refer also just to the main query. But by the aid of the get_adjacent_post filters I was able to get my posts sorted again as well as only show todays as well as future posts by using:

    function mv_adjacent_post_join() {
        global $wpdb;
        return "INNER JOIN $wpdb->postmeta AS mvpm ON mvpm.post_id = p.ID";
    }
    add_filter( 'get_previous_post_join', 'mv_adjacent_post_join' );
    add_filter( 'get_next_post_join', 'mv_adjacent_post_join' );
    
    function mv_previous_post_where() {
        global $wpdb, $post;
        $mv_event_start = date( 'Y-m-d H:i:s', strtotime( '-8 hours' ) );
        return $wpdb->prepare( " WHERE mvpm.meta_key = 'event_start' AND CAST(mvpm.meta_value AS DATETIME) >= %s AND p.post_type = 'post' AND p.post_status = 'publish'", $mv_event_start );
    }
    add_filter( 'get_previous_post_where', 'mv_previous_post_where' );
    
    function mv_next_post_where() {
        global $wpdb, $post;
        $mv_event_start = date( 'Y-m-d H:i:s', strtotime( '-8 hours' ) );
        return $wpdb->prepare(" WHERE mvpm.meta_key = 'event_start' AND CAST(mvpm.meta_value AS DATETIME) >= %s AND p.post_type = 'post' AND p.post_status = 'publish'", $mv_event_start );
    }
    add_filter( 'get_next_post_where', 'mv_next_post_where' );
    
    function mv_previous_post_sort() {
        return "ORDER BY mvpm.meta_value DESC LIMIT 1";
    }
    add_filter( 'get_previous_post_sort', 'mv_previous_post_sort' );
    
    function mv_next_post_sort() {
        return "ORDER BY mvpm.meta_value ASC LIMIT 1";
    }
    add_filter( 'get_next_post_sort', 'mv_next_post_sort' );

    The only problem left get_adjecant_postsdoesn’t really know what the next and previous post is each time. :/ In wp-includes/link-template.php it is accomplished by comparing p.post_date with $post->post_datebut for me those two look basically the same. But in my case i would need to compare the event_startdate time field of the current post to the date and time of the previous and next post within the WHERE filter? But how do I determine the ID of the next and previous post easily and apply it to the line of SQL in the WHERE filter?

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.