This function is a function in WordPress, the function is to get adjacent post articles.
The function is not large, the valid code is about 70 lines, but it contains a lot of knowledge, so specifically with an article to explain.
The source code for the Get_adjacent_post function is located in wp-includes/link-template.php.
I will read the notes through the "//roc:" in the source code.
/**
* Retrieve adjacent post.
*
* Can either be next or previous post.
*
&N bsp;* @since 2.5.0
*
* @param bool $in _same_cat Optional. Whether post should be in a same category. The
* @param array|string $excluded _categories Optional. Array or comma-separated List of excluded category IDs. The
* @param bool $previous Optional. Whether to retrieve previous post.
* @return Mixed Post Object if Successful. Null If global $post is not set. Empty string If no corresponding post exists.
*/
"notes"
The above section is a description of the function, which includes three parameters:
1 $in _same_cat parameter to indicate whether it needs to be in the same category, default to False.
2 $excluded _categories parameter, which is used to set the post in which category is ignored. You can assign a value by composing an array or comma-separated list with the category ID.
3 $previous parameter that indicates whether the previous post is extracted. The default is true. If you want to extract the post on the next post, you will need to set to false.
The return value of this function also has three different cases:
1 returns the Post object, indicating success;
2 returns NULL, it indicates that the global $post is not set;
3 returns an empty string, indicating that the corresponding post does not exist.
function Get_adjacent_post ($in _same_cat = False, $excluded _categories = "", $previous = True) {
Global $wpdb;
Notes
Here declared $wpdb global variables, this variable is actually very important, it is WordPress itself for developers to provide the public global variables, developers can directly use this function to operate the database, including new, delete, add, update and so on.
It should be noted that if you want to use this "universal key", you need to declare this variable in your own function.
In addition, under normal circumstances, the $WPDB variable has only access to a database corresponding to the blog and does not have permissions on other databases.
For example, to query the contents of a table in a database, you can do this:
if (! $post = Get_post ()) return null;
$current _post_date = $post->post_date;
$join = "";
$posts _in_ex_cats_sql = "";
if ($in _same_cat | |! empty ($excluded _categories)) {
$join = "INNER join $wpdb->term_relationships as tr on p.id = tr.object_id INNER join $wpdb->term_taxonomy TT on TR . term_taxonomy_id = tt.term_taxonomy_id ";
if ($in _same_cat) {
if (! is_object_in_taxonomy ($post->post_type, "category"))
return "";
$cat _array = wp_get_object_terms ($post- >id, "category", Array ("Fields" => "IDs"));
if (! $cat _array | | is_wp_error ($CAT _array))
return "";
$join. = "and tt.taxonomy =" category "and tt.term_id in (". Implode (",", $cat _array). ")";
}
$posts _in_ex_cats_sql = "and tt.taxonomy =" category "";
if (! empty ($excluded _categories)) {
if (! Is_array ($excluded _categories)) {
Back-compat, $excluded _categories used to is IDs separated by "and"
if (Strpos ($excluded _categories, "and")!== false) {
_deprecated_argument (__function__, "3.3", sprintf ("Use commas instead of%s to separate excluded."), "" A nd ""));
$excluded _categories = Explode ("and", $excluded _categories);
} else {
$excluded _categories = Explode (",", $excluded _categories);
}
}
$excluded _categories = Array_map ("Intval", $excluded _categories);
if (! empty ($cat _array)) {
$excluded _categories = Array_diff ($excluded _categories, $cat _array);
$posts _in_ex_cats_sql = "";
}
if (!empty ($excluded _categories)) {
$posts _in_ex_cats_sql = "and tt.taxonomy =" category "and tt.term_id not in (". Implode ($excluded _categories, ","). ")";
}
}
}
$adjacent = $previous? "Previous": "Next";
$op = $previous? "<": ">";
$order = $previous? "DESC": "ASC";
$join = apply_filters ("get_{$adjacent}_post_join", $join, $in _same_cat, $excluded _ categories);
$where = apply_filters ("get_{$adjacent}_post_where", $wpdb->prepare ("where P.post_date $op %s and P.post_type =%s and p.post_status = "Publish" $posts _in_ex_cats_sql, $current _post_date, $post->post_type), $i N_same_cat, $excluded _categories);
$sort = apply_filters ("get_{$adjacent}_post_sort", "Order by p.post_date $order LIMIT 1");
$query = "Select P.id from $wpdb->posts as P $join $where $sort";
$query _key = "Adjacent_post_". MD5 ($query);
$result = Wp_cache_get ($query _key, "counts");
if (false!== $result) {
if ($result)
$result = Get_post ($result);
return $result;
}
$result = $wpdb->get_var ($query);
if (null = = $result)
$result = "";
Wp_cache_set ($query _key, $result, "counts");
if ($result)
$result = Get_post ($result);
return $result;
}