Wordpress订单“Rand” - 每次使用ajax都会得到相同的结果

时间:2017-05-31 17:16:26

标签: jquery ajax wordpress

编辑:我发现了这个问题。我有一个插件,可以在后端订购帖子。它可以防止ajax调用中随机排序的帖子

我通过AJAX请求一堆随机帖子,但结果总是相同的结果。 Ajax请求是以某种方式缓存还是我遗漏了什么?

我的响应标题在开发者控制台中显示startActivityForResult,所以我猜它必须与wordpress有关?

Cache-Control:no-cache, must-revalidate, max-age=0

我的AJAX调用如下所示:

function loadmore() {
  $args = array(
    'post_type' => 'post',
    'orderby' => 'rand',
    'posts_per_page' => 8,
    'post_status' => 'publish',
  );

  $the_query = new WP_Query( $args );
  $i = 1;
  $posts = array();

  if ( $the_query->have_posts() ):
    while ( $the_query->have_posts() ):
      $post = $the_query->the_post();

      $categories = get_the_category(); 
      $cat_name = $categories[0]->cat_name;

      $posts[] = array(
        'permalink' => get_permalink(),
        'thumbnail' => get_the_post_thumbnail($post, 'square-medium'),
        'post_format' => get_post_format(),
        'preview_bg' => get_field('preview_bg'),
        'preview_text' => get_field('preview_text'),
        'preview_color' => get_field('preview_color'),
        'smaller_quote' => get_field('smaller_quote'),
        'cat_name' => $cat_name,
        'title' => get_the_title(),
      );

    endwhile;
    wp_reset_postdata();
  endif;


    wp_send_json($posts);
    die();
}

1 个答案:

答案 0 :(得分:0)

您可以在参数中再传递一个参数,例如

    remove_all_filters('posts_orderby');
        $args = array(
            'orderby' => 'rand',
            'order'    => 'ASC'
        );
 $the_query = new WP_Query( $args );
相关问题