这与this question.
我们必须加入check_ajax_referer
仅处理内部链接功能。然后我们为注册一个操作pre_get_posts
将搜索扩展到草稿和待处理的帖子。
我们仍然不会得到很好的永久链接,因为它们被排除在get_permalink
. 因此,我们也为此注册了一个过滤器,并再次向WordPress请求永久链接,但带有伪造的帖子状态。
As代码:
add_action( \'check_ajax_referer\', \'internal_links_for_drafts\' );
/**
* Extend search for internal links to \'draft\' and \'pending\' statuses.
*
* @wp-hook check_ajax_referer
* @wp-hook pre_get_posts
* @param string|WP_Query $var
* @return boolean|void
*/
function internal_links_for_drafts( $var )
{
if ( \'check_ajax_referer\' === current_filter() && \'internal-linking\' === $var )
return add_action( \'pre_get_posts\', __FUNCTION__ );
// now we are in the \'pre_get_posts\' action.
$var->set( \'post_status\', array( \'publish\', \'pending\', \'draft\' ) );
remove_action( \'pre_get_posts\', __FUNCTION__ );
add_filter( \'post_link\', \'draft_permalink\', 10, 2 );
}
/**
* Get permalink for drafts and pending posts.
*
* Dangerous, because their title can still change.
*
* @param string $permalink
* @param object $post
* @return string
*/
function draft_permalink( $permalink, $post )
{
remove_filter( current_filter(), __FUNCTION__ );
// swap status temporary
$original_status = $post->post_status;
$post->post_status = \'publish\';
$url = get_permalink( $post );
$post->post_status = $original_status;
return $url;
}