How to Show random post on WP, except the last pos

2019-05-31 06:38发布

I was able to locate part of the answer, I'm new to Wordpress. Im looking to show random post on the main page, but except the last post i found this

add_action('pre_get_posts', 'my_pre_get_posts');
function my_pre_get_posts($query) {
    if ( $query->is_home() && $query->is_main_query() ) {
        $query->set('orderby', 'rand');
    }
}

Thank you in advance

标签: php wordpress
1条回答
趁早两清
2楼-- · 2019-05-31 07:06


Here is the code please use this code in functions.php file and enter the last post id which you need to exclude the post.

add_action('pre_get_posts','my_pre_get_posts');
function my_pre_get_posts($query) {
if ( $query->is_home() && $query->is_main_query() ) {
$query->set('orderby', 'rand');
$query->set('post__not_in',array(1));
}
}
Please use it and I 'm also searching a best possible solution in which you need not to enter the static last post id =1 .
Thanking you.

查看更多
登录 后发表回答