You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

获取近2周自定义文章类型随机条目重复问题求助

Hey there! Let's figure out why your random posts aren't actually random—super frustrating when that happens, right?

First, let's break down the possible issues and fixes step by step:

1. Double-check your query's result set size

The most common reason you're seeing the same 3 posts every time is that your date_query + tax_query is only returning 3 posts total, not 10. If there's only 3 posts that match your "last 2 weeks" and taxonomy filters, WordPress can't pick anything else!

To test this, add a quick check to see how many posts your query is actually pulling:

$test_args = array(
    'post_type' => 'country',
    'post_status' => 'publish',
    'fields' => 'ids', // Only grab IDs to keep it light
    'date_query' => array( array( 'after' => '2 weeks ago' ) ),
    'tax_query' => array( /* Your existing tax query here */ )
);
$matching_posts = get_posts($test_args);
echo 'Total matching posts: ' . count($matching_posts); // This should be 10!

If this outputs 3 instead of 10, you need to adjust your date_query or tax_query:

  • For date_query: Double-check your WordPress timezone settings (Settings > General) vs your server timezone—sometimes relative dates like "2 weeks ago" get messed up here. Try replacing 'after' => '2 weeks ago' with a hardcoded date like 'after' => date('Y-m-d', strtotime('-2 weeks')) to rule out timezone issues.
  • For tax_query: Make sure your taxonomy name is correct, and your term IDs/slugs are matching what you expect. A typo here can filter out way more posts than you intend.

2. Fix WordPress' orderby=rand caching issues

If your test shows you do have 10 matching posts, the problem is likely caching. WordPress (and many caching plugins) love to cache query results, even when you use rand—which defeats the whole purpose!

The most reliable fix is to manually randomize your posts instead of relying on WordPress' rand order:

// Step 1: Grab all IDs of posts that match your criteria
$all_valid_ids = get_posts(array(
    'post_type' => 'country',
    'post_status' => 'publish',
    'fields' => 'ids',
    'date_query' => array( array( 'after' => '2 weeks ago' ) ),
    'tax_query' => array( /* Your existing tax query here */ )
));

// Step 2: Shuffle the IDs and pick the first 3
shuffle($all_valid_ids);
$random_3_ids = array_slice($all_valid_ids, 0, 3);

// Step 3: Query those 3 random IDs
$final_args = array(
    'post_type' => 'country',
    'post_status' => 'publish',
    'post__in' => $random_3_ids,
    'orderby' => 'post__in', // Keeps the random order we just set
    'posts_per_page' => 3
);
$random_posts = get_posts($final_args);

This method bypasses WordPress' internal query caching for random results because we're manually picking the posts first.

3. Check for page caching plugins

If you're using a plugin like WP Rocket, W3 Total Cache, or Cloudflare APO, these might be caching the entire page—so even if your query is random, the cached page gets served every time.

To fix this:

  • Exclude the page where these posts appear from caching (check your plugin's settings for "exclude pages" or "dynamic content" options).
  • If your plugin supports it, mark this specific query as dynamic so it doesn't get cached.

内容的提问来源于stack exchange,提问作者devofash

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:48:46