WordPress.org

Plugin Directory

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.

Functions

  • <?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.

Arguments

  • $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.

Examples

  • (See Description section for an additional example.)

  • Similar to previous example, for WP 3.0+

<?php
$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' );
?>

Filters

The plugin is further customizable via two hooks. Typically, these customizations would be put into your active theme's functions.php file, or used by another plugin.

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.

Arguments:

  • $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.

Example:

// Always preserve the condition of the WP_Query object
add_filter( 'inject_query_posts_preserve_query_obj', 'my_preserve_query_obj', 10, 4 );
function my_preserve_query_obj( $preserve_query_obj, $query_obj, $posts, $config ) {
    return true;
}

c2c_inject_query_posts (action)

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.

Arguments:

  • The same arguments as c2c_inject_query_posts()

Example:

Instead of:

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

Do:

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

Requires: 3.6 or higher
Compatible up to: 4.1.5
Last Updated: 2015-2-12
Active Installs: 10+

Ratings

0 out of 5 stars

Support

Got something to say? Need help?

Compatibility

+
=
Not enough data

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