Plugin Directory

Test out the new Plugin Directory and let us know what you think.

Inject Query Posts

Inject an array of posts into a WP query object as if queried, particularly useful to allow use of standard template tags.

Template Tags

The plugin provides one template tag for use in your theme templates, functions.php, or plugins.


  • <?php function c2c_inject_query_posts( $posts, $config = array(), $query_obj = null, $preserve_query_obj = true, $cache_posts = true ) ?> Injects an array of posts into a query object as if that query object had obtained those posts via a query.


  • $posts (array) Array of posts to inject into the query object.

  • $config (array) Optional. Associative array of query object variables to directly set, and their values.

  • $query_obj (WP_Query|null) Optional. The query object to modify. If null, then the global wp_query object will be used. Pass a string or non-zero integer to have a new query object created and used.

  • $preserve_query_obj (bool) Optional. Should the query object be kept as-is prior to injecting posts? Default is false. If false, then the object is re-initialized/reset before post injection.

  • $cache_posts (bool) Optional. Update the posts in cache? Default is true.


  • (See Description section for an additional example.)

  • Similar to previous example, for WP 3.0+

$posts = c2c_get_random_posts( 5, '' ); // Obtain posts via your favorite related posts, find posts, etc plugin, or via custom query
do_action( 'c2c_inject_query_posts', $posts ); // Better than direct call to c2c_inject_query_posts( $posts );
get_template_part( 'loop' );


The plugin is further customizable via two hooks. Such code should ideally be put into a mu-plugin or site-specific plugin (which is beyond the scope of this readme to explain).

inject_query_posts_preserve_query_obj (filter)

The 'inject_query_posts_preserve_query_obj' filter allows you override the value of the $preserve_query_obj argument passed to the function. This is not typical usage for most users.


  • $preserve_query_obj (bool) : Boolean indicating if the query object was set to be preserved or not
  • $query_obj (WP_Query object) : The WP_Query object passed to the c2c_inject_query_posts()
  • $posts (array) : The posts being injected into the WP_Query object
  • $config (array) : Query object variables to directly set, and their values.


 * Always preserve the condition of the WP_Query object passed ot Inject Query Posts.
 * @param bool     $preserve_query_obj The default preservation value as passed to the function.
 * @param WP_Query $query_obj          The query object.
 * @param array    $posts              The posts being injected.
 * @param array    $config             Associative array of query object variables to directly set, and their values.
 * @return bool
function my_preserve_query_obj( $preserve_query_obj, $query_obj, $posts, $config ) {
    return true;
add_filter( 'inject_query_posts_preserve_query_obj', 'my_preserve_query_obj', 10, 4 );

c2c_inject_query_posts (filter)

The 'c2c_inject_query_posts' filter allows you to use 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.


  • The same arguments as c2c_inject_query_posts()


Instead of:

<?php echo c2c_inject_query_posts( $posts, array( 'is_search' => true ) ); ?>


<?php echo apply_filters( 'c2c_inject_query_posts', $posts, array( 'is_search' => true ) ); ?>

Requires: 3.6 or higher
Compatible up to: 4.7.3
Last Updated: 1 month ago
Active Installs: 10+


0 out of 5 stars


Got something to say? Need help?


Not enough data

0 people say it works.
0 people say it's broken.