我已经在WordPress主题(wue-theme.app)中实现了Vue.js单页应用程序,并希望通过单帖子模板中的下一个/上一个链接浏览博客帖子。但是我很难从链接中删除私人帖子-没有可用的WP_Query选项。那么,如何浏览状态仅为“发布”的帖子?
答案 0 :(得分:0)
不幸的是,我找不到任何记录的“官方”方式来做到这一点。因此,我对默认的SQL查询进行了一些修改,您可以幸运地对其应用过滤器,并从查询中删除了“私有” post_status。这是您必须在functions.php中放置的“ post”帖子类型的完整自定义代码:
add_filter( 'rest_prepare_post', 'technomad_post_api_hook', 10, 3 );
function technomad_post_api_hook( $response ) {
if ( isset( $_REQUEST["slug"] ) ) {
add_filter( "get_next_post_where", function($query, $in_same_cat, $excluded_categories, $taxonomy, $post){
$query = str_replace("OR p.post_status = 'private'", "", $query);
return $query;
}, 100, 5 );
add_filter( "get_previous_post_where", function($query, $in_same_cat, $excluded_categories, $taxonomy, $post){
$query = str_replace("OR p.post_status = 'private'", "", $query);
return $query;
}, 100, 5 );
$next = get_adjacent_post( false, '', false );
$previous = get_adjacent_post( false, '', true );
if ( is_a( $next, 'WP_Post' ) ) {
$response->data['next'] = array(
"id" => $next->ID,
"slug" => $next->post_name,
"title" => $next->post_title,
);
} else {
$response->data['next'] = null;
}
if ( is_a( $previous, 'WP_Post' ) ) {
$response->data['previous'] = array(
"id" => $previous->ID,
"slug" => $previous->post_name,
"title" => $previous->post_title,
);
} else {
$response->data['previous'] = null;
}
}
}
您也可以只返回自己的自定义查询,而不是str_replace():
return $wpdb->prepare( "WHERE p.post_title > %s AND p.post_type = %s AND ( p.post_status = 'publish' )", $post->post_title, $post->post_type );