首页 > 解决方案 > 未登录用户的 WP_query 私人帖子(在 WordPress 5.7 中更改)

问题描述

我正在使用此代码查询所有私人帖子:

$wpb_all_query = new WP_Query(array('post_type'=>'post', 'post_status'=>'private', 'posts_per_page'=>-1));

在 WordPress 5.7 之前,此代码显示所有私人帖子,即使对于未登录的用户也是如此。然后我只是通过 the_content 过滤器隐藏了这个端口上的链接。正在帮助编写带有交叉链接的大量帖子的功能。但现在这段代码不起作用:

function no_working_links_list( $content ) {
    $wpb_all_query = new WP_Query(array('post_type'=>'post', 'post_status'=>'private', 'posts_per_page'=>-1));

    if ( $wpb_all_query->have_posts() ) :
        while ( $wpb_all_query->have_posts() ) : $wpb_all_query->the_post();
        $linker = get_permalink();
        $content = no_working_links( $content, $linker );
        endwhile; 
        wp_reset_postdata(); 
    endif;
    return $content;
}
add_filter( 'the_content', 'no_working_links_list' ); 

function no_working_links( $content, $linker ) { 
    $pattern = '~(<a href="' .$linker. '">)([^<]*)(</a>)~';
    $content = preg_replace($pattern, '$2', $content);

   

    return $content;
}

也许有人有想法?

标签: wordpress

解决方案


发现,上面示例中的错误不在 wp_query 中。从 WP 5.7 开始,私人帖子的 get_permalink($ID) 将显示:

https://example.com/pretty/link/如果用户登录

https://example.com/?p=100如果用户未登录


推荐阅读