问题描述
如何从循环之外获取上一个和下一个帖子?除了自定义 SELECT 查询 (使用 get_posts
或 WP_Query
) 之外,我尝试的一切都将在页面上进一步打破事情。
我目前有一个递归函数,希望能找到满足一定条件的第一个”previous” 文件。我抓住第一个值从 get_previous_post()
测试,但不知道如何获得 previous_previous_post()
。
add_action( 'wp_ajax_nopriv_myajax-submit', 'ajax_project_load' );
add_action( 'wp_ajax_myajax-submit', 'ajax_project_load' );
function ajax_project_load() {
$the_slug = $_POST['slug'];
$args=array(
'name' => $the_slug,
'post_type' => 'projects',
'post_status' => 'publish',
'showposts' => 1,
'ignore_sticky_posts' => 1
);
$my_posts = get_posts($args);
if( $my_posts ) :
global $post;
$post = $my_posts[0];
$postCapabilityFilter = $_POST['capFilter']=='undefined' ? $_POST['capFilter'] : substr($_POST['capFilter'], 1);
$response = json_encode( "Success" );
header( "Content-Type: application/json" );
$next_post = get_previous_post();
function filtered_next_post($next_post){
if($next_post){
$next_slug = $next_post->post_name;
$next_ID = $next_post->ID;
global $wpdb;
global $postCapabilityFilter;
$querystr = "
SELECT $wpdb->postmeta.*
FROM $wpdb->posts, $wpdb->postmeta
WHERE $wpdb->posts.post_type = 'projects'
AND $wpdb->posts.post_status = 'publish'
AND $wpdb->posts.ID = $next_ID
AND $wpdb->posts.ID = $wpdb->postmeta.post_id
AND $wpdb->postmeta.meta_key = '_slideshow_content'
ORDER BY $wpdb->posts.post_name DESC
";
$nextProjQuery = $wpdb->get_results($querystr, OBJECT);
foreach($nextProjQuery as $nextProj):
$nextProjMetaArr = unserialize($nextProj->meta_value);
foreach ($nextProjMetaArr['slides'] as $npSlideArr){
echo 'and..';
if ( !in_array( $postCapabilityFilter, $npSlideArr['slideCap'] ) ){
echo 'not in it..';
//$next_post = get_previous_previous_post();
//filtered_next_post($next_post);
}
}
endforeach;
return $next_slug;
}
}
$next_slug = filtered_next_post($next_post);
最佳解决方案
看看 get_previous_post()
和 get_next_post()
,你会看到他们都使用 get_adjacent_post()
来查找上一个或下一个帖子。
假设您想要根据当前帖子的 ID 获取上一个帖子的 ID 。这是你会做的:
function get_previous_post_id( $post_id ) {
// Get a global post reference since get_adjacent_post() references it
global $post;
// Store the existing post object for later so we don't lose it
$oldGlobal = $post;
// Get the post object for the specified post and place it in the global variable
$post = get_post( $post_id );
// Get the post object for the previous post
$previous_post = get_previous_post();
// Reset our global object
$post = $oldGlobal;
if ( '' == $previous_post ) {
return 0;
}
return $previous_post->ID;
}
你可以做一个类似的事情来获取下一个帖子的 ID … 你可以递归地执行,如果你需要获得上一个上一篇文章:
$two_posts_ago = get_previous_post_id( get_previous_post_id( $post->ID ) );
长文慎入
基本上,get_previous_post()
和 get_next_post()
都参考全局 $post
对象进行选择。在调用这两个函数之前,您需要设置此对象,否则它们将不会知道要用作下一个/前一个参考的帖子。
上面的包装功能只是基于 passed-in ID 设置了全局 $post
。你可以让它返回上一个帖子的整个对象,而不是 ID,这完全取决于你。
次佳解决方案
在这里,您可以使用自定义 sql 查询& 与过滤器 get_{$adjacent}_post_where
其中默认相邻是以前。也取决于 $current_post_date
& 比较运算符 $op
。
function bm_get_adjacent_post( $post_id, $author_id, $previous = 1 ) {
global $wpdb;
if ( ( ! $post = get_post( $post_id ) ) )
return null;
$current_post_date = $post->post_date;
$adjacent = $previous ? 'previous' : 'next';
$op = $previous ? '<' : '>';
$order = $previous ? 'DESC' : 'ASC';
$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' AND p.post_author = %d", $current_post_date, 'projects' , $author_id ), '', '' );
$sort = apply_filters( "get_{$adjacent}_post_sort", "ORDER BY p.post_date $order LIMIT 1" );
$query = "SELECT p.ID FROM $wpdb->posts AS p $where $sort";
//echo $query;
$result = $wpdb->get_var( $query );
if ( null === $result )
$result = '';
if ( $result )
$result = get_post( $result );
return $result;
}
参考文献
注:本文内容整合自 Google/Baidu/Bing 辅助翻译的英文资料结果。如果您对结果不满意,可以加入我们改善翻译效果:薇晓朵技术论坛。