Here is what I have (just is rough draught for testing). This successfully retrieves the ID of the adjacent post and returns it according to the query_vars
set in the URL. If no custom query_vars
are set, the function gets anthe adjacent post
$where = ''; in normal chronological order as normal
$join<?php = '';
$opfunction get_adjacent_include_referred_post( $previous = '>';true ) {
$order global $wpdb;
if ( ( ! $post = 'ASC';get_post() ) )
return null;
$current_post_date = $post->post_date;
$post_type
$join = $post'';
$where = '';
//Alter the SQL query if 'aq' is set in single page URL
if( is_author_referrer() ) {
$author = $_GET['aq'];
$where .= $wpdb->post_type;>prepare( "AND p.post_author IN (%s)", $author );
$category
}
//Alter the SQL query if 'tq' is set in single page URL
if( is_tax_referrer() ) {
$term = 1;$_GET['tq'];
$join = $wpdb->prepare( " INNER JOIN $wpdb->term_relationships AS tr ON p.ID = tr.object_id AND tr.term_taxonomy_id IN (%s)", $term );
}
//Alter the the SQL query if sq is set in single page URL
if( is_search_referrer() ) {
$term = $_GET['sq'];
// **MISSING INFO**
}
//Set next and previous post's operators to form the SQL query
$adjacent = $previous ? 'previous' : 'next';
$op = $previous ? '<' : '>';
$order = $previous ? 'DESC' : 'ASC';
// Setting the default clauses to build the custom SQL query to get adjacent post
$where = $wpdb->prepare( "WHERE p.post_date $op %s AND p.post_type = %s AND p.post_status = 'publish' $where", $current_post_date, $post_type$post->post_type );
$sort = "ORDER BY p.post_date $order LIMIT 1";
$query = $wpdb->get_results("SELECT p.ID FROM $wpdb->posts AS p $join $where $sort");$sort";
Now, to get adjacent posts from posts belonging to the same, for tag, category or term according to the value in the URL, I can simply add a join like this
$join
= $wpdb->prepare( " INNER//Create JOINand $wpdb->term_relationshipsset AScache trto ONimprove p.IDperformance of the function
$query_key = tr'get_the_adjacent_post_' .object_id md5( AND$query tr.term_taxonomy_id);
IN $result = wp_cache_get(%s)" $query_key, $term'counts' );
or for authors
$where . $result = $wpdb->prepare>get_var( "AND$query p.post_author);
IN if (%s null === $result )"
$result = '';
wp_cache_set( $query_key, $author$result, 'counts' );
return $result;
}
This works fineMy problem, but I cannot get anything to work for a search query (my SQL knowledge is just to poor to assemble something useful). If my search query is rocks
and someone clicks on a single post, how do I adjust my SQL query to get the next and previousadjacent post in myfrom the current single post being viewed within the same search query rocks
?
FEW NOTES ON MY CODE
The is_***_referrer()
conditional checks are wrapper functions which checks if the appropriate S_GET['***']
is set. For the search query, is_search_referrer()
checks if $_GET['sq']
is set, returns true if the query_vars
exists, false on failure
The value of $_GET['sq']
will is the value of get_search_query()
. This is the value that should be used to get the adjacent post for the same search query
There is no syntax errors in my code and it works. I know the code looks funny for some reason in the code block above, and it does seem from that that there are syntax errors, but there aren't.