Title: Inject Query Posts
Author: Scott Reilly
Published: <strong>2010 年 9 月 27 日</strong>
Last modified: 2025 年 4 月 23 日

---

搜索插件

![](https://ps.w.org/inject-query-posts/assets/banner-772x250.png?rev=824148)

![](https://ps.w.org/inject-query-posts/assets/icon-128x128.png?rev=972934)

# Inject Query Posts

 作者：[Scott Reilly](https://profiles.wordpress.org/coffee2code/)

[下载](https://downloads.wordpress.org/plugin/inject-query-posts.3.0.5.zip)

 * [详情](https://cn.wordpress.org/plugins/inject-query-posts/#description)
 * [评价](https://cn.wordpress.org/plugins/inject-query-posts/#reviews)
 *  [安装](https://cn.wordpress.org/plugins/inject-query-posts/#installation)
 * [开发进展](https://cn.wordpress.org/plugins/inject-query-posts/#developers)

 [支持](https://wordpress.org/support/plugin/inject-query-posts/)

## 描述

This plugin provides a function for use by developers who have their own code for
fetching posts according to a given criteria and now want to make use of loop-aware
template tags to display those posts.

WordPress’s template tags are intended to be used within ‘the loop’. The loop is
managed by a WP_Query object which sets up various global variables and its own 
object variables for use by the various template tags. The primary purpose of a 
WP_Query object is to actually query the database for the posts that match the currently
specified criteria. However, if you don’t need to query for posts since you already
have them by some other means, you can still take advantage of the template tags
by injecting those posts into the WP_Query via this plugin.

Depending on the template tags you are looking to use, or the logic you are hoping
to employ within a loop, you may need to manually configure some of the query object’s
variables.

如：

    ```
    <?php // Say we're in the sidebar

    // We've gotten some post objects on our own.
    $posts = c2c_get_random_posts( 5, '' );

    // Inject the posts
    c2c_inject_query_posts( $posts );

    // Now let's display them via template tags:
    if ( have_posts() ) :
        while ( have_posts() ) : the_post(); ?>

            <li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>

        <?php endwhile;?>
    <?php endif; ?>
    ```

Links: [Plugin Homepage](https://coffee2code.com/wp-plugins/inject-query-posts/)
| [Plugin Directory Page](https://wordpress.org/plugins/inject-query-posts/) | [GitHub](https://github.com/coffee2code/inject-query-posts/)
| [Author Homepage](https://coffee2code.com)

### Developer Documentation

Developer documentation can be found in [DEVELOPER-DOCS.md](https://github.com/coffee2code/inject-query-posts/blob/master/DEVELOPER-DOCS.md).
That documentation covers the template tag and hooks provided by the plugin.

As an overview, this is the template tag provided by the plugin:

 * `c2c_inject_query_posts()` : Template tag to inject an array of posts into a 
   query object as if that query object had obtained those posts via a query.

These are the hooks provided by the plugin:

 * `inject_query_posts_preserve_query_obj` : Overrides the value of the `$preserve_query_obj`
   argument passed to the function. This is not typical usage for most users.
 * `c2c_inject_query_posts` : Allows use of an alternative approach to safely invoke`
   c2c_inject_query_posts()` in such a way that if the plugin were deactivated or
   deleted, then your calls to the function won’t cause errors in your site.

## 安装

 1. Install via the built-in WordPress plugin installer. Or install the plugin code
    inside the plugins directory for your site (typically `/wp-content/plugins/`).
 2. Activate the plugin through the ‘Plugins’ admin menu in WordPress
 3. Use the `c2c_inject_query_posts()` function to inject an array of posts into a 
    WP query object. Specify the posts array as the first argument. Configure the query
    object by passing an array as the second argument. If specifying a WP query object,
    pass it as the third object; if not specified then the global wp_query object will
    be assumed.

## 评价

此插件暂无评价。

## 贡献者及开发者

「Inject Query Posts」是开源软件。 以下人员对此插件做出了贡献。

贡献者

 *   [ Scott Reilly ](https://profiles.wordpress.org/coffee2code/)

[帮助将「Inject Query Posts」翻译成简体中文。](https://translate.wordpress.org/projects/wp-plugins/inject-query-posts)

### 对开发感兴趣吗?

您可以[浏览代码](https://plugins.trac.wordpress.org/browser/inject-query-posts/)，
查看[SVN仓库](https://plugins.svn.wordpress.org/inject-query-posts/)，或通过[RSS](https://plugins.trac.wordpress.org/log/inject-query-posts/?limit=100&mode=stop_on_copy&format=rss)
订阅[开发日志](https://plugins.trac.wordpress.org/log/inject-query-posts/)。

## 更新日志

#### 3.0.5 (2025-04-22)

 * Change: Note compatibility through WP 6.8+
 * Change: Note compatibility through PHP 8.3+
 * Change: Update copyright date (2025)
 * Unit tests:
    - Change: Explicitly define return type for overridden method

#### 3.0.4 (2024-08-02)

 * Change: Note compatibility through WP 6.6+
 * Change: Update copyright date (2024)
 * New: Add `.gitignore` file
 * Change: Reduce number of ‘Tags’ from `readme.txt`
 * Change: Remove development and testing-related files from release packaging
 * Unit tests:
    - Hardening: Prevent direct web access to `bootstrap.php`
    - Allow tests to run against current versions of WordPress
    - New: Add `composer.json` for PHPUnit Polyfill dependency
    - Change: In bootstrap, store path to plugin directory in a constant

#### 3.0.3 (2023-05-18)

 * Change: Add link to DEVELOPER-DOCS.md to README.md
 * Change: Tweak installation instruction
 * Change: Tweak some documentation text spacing and fix a typo
 * Change: Note compatibility through WP 6.3+
 * Change: Update copyright date (2023)
 * New: Add a potential TODO feature

_Full changelog is available in [CHANGELOG.md](https://github.com/coffee2code/inject-query-posts/blob/master/CHANGELOG.md)._

## 额外信息

 *  版本 **3.0.5**
 *  最后更新：**1 年前**
 *  活跃安装数量 **10+**
 *  WordPress 版本 ** 3.6 或更高版本 **
 *  已测试的最高版本为 **6.8.5**
 *  语言
 * [English (US)](https://wordpress.org/plugins/inject-query-posts/)
 * 标签
 * [loop](https://cn.wordpress.org/plugins/tags/loop/)[posts](https://cn.wordpress.org/plugins/tags/posts/)
   [query](https://cn.wordpress.org/plugins/tags/query/)[template tags](https://cn.wordpress.org/plugins/tags/template-tags/)
   [wp_query](https://cn.wordpress.org/plugins/tags/wp_query/)
 *  [高级视图](https://cn.wordpress.org/plugins/inject-query-posts/advanced/)

## 评级

尚未提交反馈。

[Your review](https://wordpress.org/support/plugin/inject-query-posts/reviews/#new-post)

[查看全部评论](https://wordpress.org/support/plugin/inject-query-posts/reviews/)

## 贡献者

 *   [ Scott Reilly ](https://profiles.wordpress.org/coffee2code/)

## 支持

有话要说吗？是否需要帮助？

 [查看支持论坛](https://wordpress.org/support/plugin/inject-query-posts/)

## 捐助

您愿意支持这个插件的发展吗?

 [ 捐助此插件 ](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=6ARCFJ9TX3522)