Skip to main content
replaced http://wordpress.stackexchange.com/ with https://wordpress.stackexchange.com/
Source Link

I'm busy rewritting a custom functionrewritting 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 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 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

added 2081 characters in body
Source Link
Pieter Goosen
  • 55.4k
  • 23
  • 115
  • 209

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.

Here is what I have (just is rough draught for testing). This gets an adjacent post

$where = '';

$join = '';
$op = '>';
$order = 'ASC';
$current_post_date = $post->post_date;
$post_type = $post->post_type;
$category = 1;
$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 );
$sort = "ORDER BY p.post_date $order LIMIT 1";
$query = $wpdb->get_results("SELECT p.ID FROM $wpdb->posts AS p $join $where $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 JOIN $wpdb->term_relationships AS tr ON p.ID = tr.object_id  AND tr.term_taxonomy_id IN (%s)", $term );  

or for authors

$where .= $wpdb->prepare( "AND p.post_author IN (%s)", $author );

This works fine, but I cannot get anything to work for a search query. If my search query is rocks, how do I get the next and previous post in my search query rocks

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

Source Link
Pieter Goosen
  • 55.4k
  • 23
  • 115
  • 209

SQL query to get adjacent posts from search query

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 (just is rough draught for testing). This gets an adjacent post

$where = '';

$join = '';
$op = '>';
$order = 'ASC';
$current_post_date = $post->post_date;
$post_type = $post->post_type;
$category = 1;
$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 );
$sort = "ORDER BY p.post_date $order LIMIT 1";
$query = $wpdb->get_results("SELECT p.ID FROM $wpdb->posts AS p $join $where $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 JOIN $wpdb->term_relationships AS tr ON p.ID = tr.object_id  AND tr.term_taxonomy_id IN (%s)", $term );  

or for authors

$where .= $wpdb->prepare( "AND p.post_author IN (%s)", $author );

This works fine, but I cannot get anything to work for a search query. If my search query is rocks, how do I get the next and previous post in my search query rocks

Any suggestions on what to add?