I'm busy rewritting a custom function I have recently written to get adjacent posts according to a tag, category, custom taxonomy term, author or search value saved in the URL
I'm using the structure of get_adjacent_post
to rewrite my function.
Here is what I have. 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 the adjacent post in normal chronological order as normal
<?php
function get_adjacent_include_referred_post( $previous = true ) {
global $wpdb;
if ( ( ! $post = get_post() ) )
return null;
$current_post_date = $post->post_date;
$join = '';
$where = '';
//Alter the SQL query if 'aq' is set in single page URL
if( is_author_referrer() ) {
$author = $_GET['aq'];
$where .= $wpdb->prepare( "AND p.post_author IN (%s)", $author );
}
//Alter the SQL query if 'tq' is set in single page URL
if( is_tax_referrer() ) {
$term = $_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->post_type );
$sort = "ORDER BY p.post_date $order LIMIT 1";
$query = "SELECT p.ID FROM $wpdb->posts AS p $join $where $sort";
//Create and set cache to improve performance of the function
$query_key = 'get_the_adjacent_post_' . md5( $query );
$result = wp_cache_get( $query_key, 'counts' );
$result = $wpdb->get_var( $query );
if ( null === $result )
$result = '';
wp_cache_set( $query_key, $result, 'counts' );
return $result;
}
My problem, 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 adjacent post from the current single post being viewed within the same search query?
FEW NOTES ON MY CODE
The
is_***_referrer()
conditional checks are wrapper functions which checks if the appropriateS_GET['***']
is set. For the search query,is_search_referrer()
checks if$_GET['sq']
is set, returns true if thequery_vars
exists, false on failureThe value of
$_GET['sq']
will is the value ofget_search_query()
. This is the value that should be used to get the adjacent post for the same search queryThere 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.
Any suggestions on what to add?