一致随机的帖子与胶粘物

时间:2013-04-15 20:05:41

标签: wordpress random pagination sticky

使用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 );
}

感谢您的建议!

2 个答案:

答案 0 :(得分:2)

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

'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 :(得分:1)

如果您不介意向所有访问者显示相同的随机帖子,您可以使用transient和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
}