定制 humanmade/hm-query-loop 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

humanmade/hm-query-loop

Composer 安装命令:

composer require humanmade/hm-query-loop

包简介

WordPress Query Loop Block Tools

README 文档

README

A WordPress plugin that extends the core Query Loop block with advanced controls for managing multiple query loops on a single page.

Features

1. Posts Per Page Override for Inherited Queries

When a Query Loop block is set to "Inherit query from template", you can now override the number of posts to display. This is useful when you want to show a different number of posts than the main query. Leave the field empty to use the default number of posts.

Editor Preview: The posts per page override is reflected in the editor preview, making it easier to see how your content will appear without needing to preview or publish the page.

2. Hide on Paginated Pages

Toggle whether the query loop should be hidden when viewing page 2 or higher (when the paged query var is greater than 1). This is useful for creating layouts where different query loops appear on the first page vs subsequent pages.

3. Exclude Already Displayed Posts

Enable this option to automatically exclude posts that have been displayed by previous query loops on the same page. This ensures no duplicate posts appear when you have multiple query loops with different layouts.

Important: The exclusion applies to all query loops rendered before the current one, regardless of whether they were visible (e.g., hidden due to pagination settings).

4. Multiple Post Templates

A single Query Loop block (non-inherited) can contain multiple core/post-template blocks, each showing a different slice of the query results. Each Post Template block gets a "Posts per template" setting in its inspector controls to control how many posts it shows.

5. Query ID Deduplication

The plugin automatically assigns unique query IDs when blocks are copy-pasted or when a page renders the same template multiple times, preventing broken post exclusion and pagination.

6. Query Presets

Register custom query configurations in PHP that can be selected from a dropdown in the block editor. This allows developers to create reusable, dynamic queries (like "Related Articles" or "Trending Posts") that content editors can easily apply to any Query Loop block.

Key Benefits:

  • Define complex query logic in PHP while keeping the editor interface simple
  • Queries work in both the editor preview and on the frontend
  • Automatically hooks into all public post types via the REST API

Installation

  1. Upload the plugin to your /wp-content/plugins/ directory
  2. Run npm install in the plugin directory
  3. Run npm run build to compile the assets
  4. Activate the plugin through the 'Plugins' menu in WordPress

Development

Build Commands

  • npm run start - Start the development build with watch mode
  • npm run build - Create a production build
  • npm run lint:js - Lint JavaScript files
  • npm run lint:css - Lint CSS/SCSS files
  • npm run format - Format all files

Testing

The plugin includes end-to-end tests using Playwright and @wordpress/scripts.

Running Tests

  1. Start the WordPress test environment:

    npm run wp-env start
  2. Run the tests:

    npm run test:e2e

Additional Test Commands

  • npm run test:e2e:debug - Run tests in debug mode
  • npm run test:e2e:watch - Run tests in watch mode (reruns on changes)
  • npm run wp-env stop - Stop the WordPress environment
  • npm run wp-env - Access wp-env commands directly

See tests/e2e/README.md for more details on the test setup and writing tests.

Usage

  1. Add a Query Loop block to your page or template
  2. In the block settings sidebar, find the "Extra Query Loop Settings" panel
  3. Configure the options as needed:
    • Query Preset: Select a predefined query configuration (only visible when presets are registered)
    • Posts per page (Override): Only visible when inheriting query - enter a number to override posts per page, or leave empty to use default
    • Hide on paginated pages: Toggle to hide this block on page 2+
    • Exclude already displayed posts: Toggle to avoid showing duplicate posts
  4. For non-inherited queries with multiple Post Template blocks, select each core/post-template and set Posts per template to control how many posts each template shows

Block Context

The plugin exposes an hmQueryLoop context object from core/query to core/post-template:

// context key: 'hmQueryLoop'
{
	perPage: number | undefined,      // Custom posts per page value
	hideOnPaged: boolean,             // Whether to hide on paginated pages
	excludeDisplayed: boolean         // Whether to exclude displayed posts
}

This context is automatically registered for the core/post-template block both in JavaScript and PHP.

Query Presets API

Register custom query presets that appear in the block editor and modify queries on both frontend and REST API requests.

// In your theme's functions.php or a plugin
add_action( 'init', function() {
    \HM\QueryLoop\QueryPresets\register_query_preset(
        'related_articles',           // Unique identifier
        'Related Articles',           // Label shown in dropdown
        function( $query_vars, $context ) {
            // $context includes:
            // - post_id: Current post ID (useful for related content)
            // - is_rest: Boolean, true when called from REST API (editor)
            // - block: Array with perPage and page values

            $related_ids = get_post_meta( $context['post_id'], 'related_posts', true );

            if ( ! empty( $related_ids ) ) {
                $query_vars['post__in'] = $related_ids;
                $query_vars['orderby'] = 'post__in';
            }

            return $query_vars;
        }
    );
});

Available Functions:

  • \HM\QueryLoop\QueryPresets\register_query_preset( $name, $label, $callback ) - Register a preset
  • \HM\QueryLoop\QueryPresets\unregister_query_preset( $name ) - Remove a preset
  • \HM\QueryLoop\QueryPresets\get_registered_presets() - Get all registered presets
  • \HM\QueryLoop\QueryPresets\get_query_preset( $name ) - Get a specific preset
  • \HM\QueryLoop\QueryPresets\apply_query_preset( $name, $query_vars, $context ) - Manually apply a preset

Example Use Case

Create a custom archive layout with different query loops:

  1. First Query Loop: Show 3 featured posts in a grid layout

    • Posts per page: 3
    • Hide on paginated pages: Yes
    • Exclude already displayed posts: No
  2. Second Query Loop: Show remaining posts in a list layout

    • Posts per page: 10
    • Hide on paginated pages: No
    • Exclude already displayed posts: Yes (excludes the 3 featured posts)

On page 1, both query loops appear. On page 2+, only the second query loop appears, continuing to exclude the 3 featured posts from page 1.

Requirements

  • WordPress 6.0 or higher
  • PHP 7.4 or higher
  • Node.js 16 or higher (for development)

Release Process

This plugin uses GitHub Actions for automated versioning and release asset creation.

Creating a Release

  1. Make sure the release branch is built and ready (assets should be in the build/ directory)
  2. Go to the GitHub repository and create a new release:
    • Click "Releases" → "Draft a new release"
    • Create a new tag (e.g., v1.2.3) from the release branch
    • Add release title and notes
    • Click "Publish release"
  3. The GitHub Action will automatically:
    • Checkout the code at the tag you created
    • Replace __VERSION__ placeholders with the actual version number
    • Commit the versioned file back to the tag
    • Create a production-ready ZIP file (excluding dev files)
    • Upload the ZIP as a release asset

The tag version (e.g., v1.2.3) will be used as the plugin version. The version number should follow semantic versioning.

License

GPL-2.0+

humanmade/hm-query-loop 适用场景与选型建议

humanmade/hm-query-loop 是一款 基于 JavaScript 开发的 Composer 扩展包,目前已累计 8.74k 次下载、GitHub Stars 达 6, 最近一次更新时间为 2025 年 11 月 06 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 humanmade/hm-query-loop 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 humanmade/hm-query-loop 我们能提供哪些服务?
定制开发 / 二次开发

基于 humanmade/hm-query-loop 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 8.74k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 6
  • 点击次数: 8
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 6
  • Watchers: 0
  • Forks: 0
  • 开发语言: JavaScript

其他信息

  • 授权协议: GPL-2.0-or-later
  • 更新时间: 2025-11-06