2017-05-31 32 views
0

編輯:我找到了問題。我有一個插件可以在後端對帖子進行排序。它可以防止阿賈克斯調用中的隨機訂購帖子WordPress的訂單「蘭德」 - 每次與AJAX相同的結果

我正在通過AJAX請求一堆隨機帖子,但結果始終是相同的結果。 Ajax請求是否以某種方式被緩存?或者我錯過了什麼?

我的回覆標題在開發者控制檯中顯示Cache-Control:no-cache, must-revalidate, max-age=0,所以我猜它肯定與wordpress有關?

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(); 
} 

我的AJAX調用看起來是這樣的:

$.ajax({ 
    url: loadrandom.ajaxurl, 
    type: 'post', 
    data: { 
    action: 'loadmore' 
    }, 
    success: function(result) { 
    // do something 
    }}); 
+0

插件可以防止這種情況的正常工作。嘗試禁用所有插件,看看是否有幫助 –

回答

0

您可以在參數傳遞一個參數一樣

remove_all_filters('posts_orderby'); 
     $args = array(
      'orderby' => 'rand', 
      'order' => 'ASC' 
     ); 
$the_query = new WP_Query($args); 
+0

沒有幫助:(奇怪的是,它在我的本地MAMP實例上工作,但在兩臺服務器上,我試過它不... –