带有便签的一致随机帖子集

问题描述 投票:0回答:2

使用 WordPress,在我的主页上,我希望能够查询整个分页中一致的随机帖子,同时仍首先显示便签。我已经尽力创建一致的流程,但我错过了像其他帖子一样随机显示的便签。

function custom_query($query) {
    global $custom_query;
    if ( $custom_query && strpos($query, 'ORDER BY RAND()') !== false ) {
        $query = str_replace( 'ORDER BY RAND()', $custom_query, $query );
    }
    return $query;
}

add_filter( 'query', 'custom_query' );
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$seed = $_SESSION['seed'];

if ( empty($seed) ) {
    $seed = rand();
    $_SESSION['seed'] = $seed;
}

global $custom_query;
$custom_query = " ORDER BY rand($seed) ";

$args = array(
    'caller_get_posts' => 1,
    'orderby' => 'rand',
    'paged' => $paged,
);

query_posts($args);
$custom_query = '';

编辑:根据您的建议,我设法使用下面的代码解决了这个问题:

$sticky_post_ids = get_option('sticky_posts');
function mam_posts_query($query) {
   global $mam_posts_query;
   if ($mam_posts_query && strpos( $query, 'ORDER BY RAND()') !== false ) {
      $query = str_replace( 'ORDER BY RAND()', $mam_posts_query, $query );
   }
   return $query;
}
add_filter( 'query','mam_posts_query' );
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$seed = date('Ymdh'); // Sets an hourly random cache
global $mam_posts_query;
$mam_posts_query = " ORDER BY rand($seed) ";

$args = array(
    'orderby' => 'rand',
    'paged' => $paged,
    'post__not_in' => get_option( 'sticky_posts' )
);
$projects = query_posts($args);
$mam_posts_query = '';

if ( $paged === 1 ) {
    $stickies = get_posts( array('include' => $sticky_post_ids) );
    $projects = array_merge( $stickies, $projects );
}

谢谢您的建议!

php wordpress random pagination sticky
2个回答
2
投票

我知道codex wp_query page中的描述有点令人困惑,但我相信您需要做的就是设置

'ignore_sticky_posts' => 0
.

在我的实验中,这是有效的,但是当然,在处理查询时,我不知道在另一个位置的何处或何时,您的查询可能会发生变化..

无论如何,如果这对你不起作用,你也可以用

获得便利贴

$sticky = get_option( 'sticky_posts' );

然后像这样设置查询:

'post__in'       => get_option('sticky_posts')

甚至如此:(注意

not_in

 $query->set( 'post__not_in', get_option( 'sticky_posts' ) );

请注意,默认情况下,即时贴仅显示在主页上。

您也可以使用双循环方法:

$stickyQuery = new WP_Query( array(
        'cat'                    => $your_category,// example   
        'ignore_sticky_posts'    => 0,
        'post__in'               => get_option( 'sticky_posts' ),
        'posts_per_page'         => -1             //Get ALL and ONLY the stickies, or how many you want
    );
while( $stickyQuery->have_posts() ) : $stickyQuery->the_post();

   //... ( Sticky Posts should show )

endwhile;
wp_reset_query();

//... ( Continue main query or start a new one excluding the last.... )

1
投票

如果您不介意向所有访问者显示相同的随机帖子,您可以使用瞬态和 get_posts():

$my_random_posts = get_transient('my_random_posts');
if (!$my_random_posts) {
  $sticky_post_ids = get_option('sticky_posts');
  $my_random_posts = get_posts(array(
                                 'exclude' => $sticky_post_ids,
                                 'orderby' => 'rand',
                               ));

  if ($sticky_post_ids) {
    $sticky_posts = get_posts(array(
                                 'include' => $sticky_post_ids,
                               ));
    $my_random_posts = array_merge($sticky_posts, $my_random_posts);
  }

  set_transient('my_random_posts', $my_random_posts , 900); # 15 minutes
}
© www.soinside.com 2019 - 2024. All rights reserved.