Linux 软件免费装
Banner图

Random Posts for Query Loop Block

开发者 ctrs
更新时间 2026年1月8日 04:27
PHP版本: 7.4 及以上
WordPress版本: 6.9
版权: GPLv2 or later
版权网址: 版权信息

标签

random gutenberg block editor orderby query loop

下载

1.0.5

详情介绍:

The Query Loop block in WordPress does not include a "Random" ordering option in the sidebar UI.\ However, WordPress core does support orderby = rand in WP_Query. The problem:\ Using "orderBy": "rand" inside a Query Loop block can cause the Site Editor preview to fail, because rand is not included in the REST API schema for posts. This plugin fixes that by extending the REST API orderby enum so the editor recognizes "rand" as valid. ✔ What this plugin does ⚠ What this plugin does not do Example use case Want your homepage, blog, quotes site, testimonials, or featured-section to show\ a different post every time the page loads? Install this plugin → open the template → switch to Code Editor → set "orderBy": "rand".

安装:

  1. Upload the plugin folder to /wp-content/plugins/random-posts-for-query-loop-block/\ or install it via Plugins → Add New → Upload Plugin.
  2. Activate the plugin via Plugins → Installed Plugins.
  3. Open the Site Editor and edit a template or page containing a Query Loop block.
  4. Switch to Code Editor\ (top-right kebab menu → Code editor).
  5. Find the wp:query block and set:
"orderBy": "rand" Example: ``` ```
  1. Save the template.
  2. The Query Loop will now return one random post on each page load, and the Site Editor preview will continue to work normally.

常见问题:

Will this add a “Random” option to the Query Loop sidebar?

No. The dropdown list is defined in Gutenberg’s JavaScript and cannot be modified by PHP alone.\ You must edit the block’s attributes in Code Editor.

Does this affect frontend queries outside the block editor?

No. It only extends the REST API schema used by the editor.\ WP_Query has natively supported orderby = 'rand' for many years.

Does this slow down my website?

No extra load is added beyond core WordPress behavior.\ However, orderby = rand can be heavy for very large datasets — this is true with or without this plugin.

更新日志:

1.0.0