Show scheduled posts in loop (but don't link to them)?

Is there a way to show scheduled posts in the regular loop but have them not link? I'd like to show that they'

Is there a way to show scheduled posts in the regular loop but have them not link? I'd like to show that they're coming soon.

This code makes them visible, but you can click into the post, which I don't want.

add_action( 'pre_get_posts', function ( $wp_query ) {
global $wp_post_statuses;

if (
    ! empty( $wp_post_statuses['future'] ) &&
    ! is_admin() &&
    $wp_query->is_main_query()
   ) {
    $wp_post_statuses['future']->public = true;
   }
});

Is there a way to show scheduled posts in the regular loop but have them not link? I'd like to show that they're coming soon.

This code makes them visible, but you can click into the post, which I don't want.

add_action( 'pre_get_posts', function ( $wp_query ) {
global $wp_post_statuses;

if (
    ! empty( $wp_post_statuses['future'] ) &&
    ! is_admin() &&
    $wp_query->is_main_query()
   ) {
    $wp_post_statuses['future']->public = true;
   }
});
Share Improve this question asked Feb 3, 2020 at 14:18 ElizabethElizabeth 3772 silver badges13 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Is there a way to show scheduled posts in the regular loop but have them not link? I'd like to show that they're coming soon.

Yes, if you don't want to link to them, then don't create links for them.

This code makes them visible, but you can click into the post, which I don't want.

pre_get_posts changes what posts a query fetches, it has nothing to do with how those posts are displayed, how they are displayed is entirely the job of the theme templates.

Oddly though, your pre_get_posts filter does not modify the query object. The consequence of this is that all following queries will have future posts, even if they aren't the main query, as long as they happen after it.

You would be much better off just modifying the query instead of changing WP globals e.g.

add_action( 'pre_get_posts', function ( $wp_query ) {
    if ( is_admin() ) {
        return;
    }
    if ( ! $wp_query->is_main_query()) {
        return;
    }
    $wp_query->set( 'post_status', [ 'publish', 'future' ] );
});

So, how do you only link to the published ones? Easy, just check the status and only link if it's publish

if ( get_post_status() == 'publish' ) {
    // show the post in the loop normally
} else {
   // it's a scheduled post, show it differently
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744780830a4593328.html

相关推荐

  • Show scheduled posts in loop (but don't link to them)?

    Is there a way to show scheduled posts in the regular loop but have them not link? I'd like to show that they'

    2天前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信