Categories

Inject Query Posts

Author: Scott Reilly
Version: 3.0.2
First released: 2010-09-27
Last update: 2021-10-02
Compatibility: WP 3.6 – 5.8.1
Download: [ zip ]
Description:

Facilitates injecting an array of posts into a WP query object as if queried. Particularly useful to allow use of standard template tags.

Extended Description

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.

Example:

<?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 | Plugin Directory Page | GitHub | Author Homepage

Developer Documentation

Developer documentation can be found in DEVELOPER-DOCS.md. That documentation covers the template tag and hooks provided by the plugin.

As an overview, this is the template tag provided 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.

Theses 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.

Find out more at the plugin’s WordPress Plugin Repository page.

Installation

  1. Install via the built-in WordPress plugin installer. Or download and unzip inject-query-posts.zip 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.

Release Log

3.0.2 (2021-10-01)

  • New: Add DEVELOPER-DOCS.md and move template tag and hooks documentation into it
  • Change: Note compatibility through WP 5.8+
  • Unit tests:
    • Change: Restructure unit test directories
      • Change: Move phpunit/ into tests/phpunit/
      • Change: Move phpunit/bin/ into tests/
    • Change: Remove ‘test-‘ prefix from unit test file
    • Change: In bootstrap, store path to plugin file constant
    • Change: In bootstrap, add backcompat for PHPUnit pre-v6.0

3.0.1 (2021-04-13)

  • Change: Note compatibility through WP 5.7+
  • Change: Update copyright date (2021)

3.0 (2020-08-14)

  • This significant release changes argument handling (while retaining backward compatibility), removes long-deprecated inject_query_posts(), changes unit test file structure, improves inline documentation, adds TODO.md file, and notes compatibility through WP 5.5+.

Details:

  • New: Add TODO.md and move existing TODO list from top of main plugin file into it
  • New: Add inline documentation for inject_query_posts_preserve_query_obj filter
  • Change: Consolidate multiple arguments for c2c_inject_query_posts() into an array
    • Change: Replace second and subsequent args with $args, a configuration array
    • Note: Legacy (pre-3.0) syntax is still supported, so existing code won’t break
  • Change: Ensure use of post objects after the incoming posts have been mapped as such
  • Change: Remove long-deprecated inject_query_posts()
  • Change: Cast first argument to inject_query_posts_preserve_query_obj filter as bool
  • Change: Move inject_query_posts_preserve_query_obj filter until after a query object has been obtained
  • Change: Restructure unit test file structure
  • Change: Note compatibility through WP 5.5+
  • Change: Add and update some inline documentation

Copyright & Disclaimer

Copyright © 2010-2021 by Scott Reilly (aka coffee2code)

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

Discussion / Support

Have any questions, comments, or suggestions? Please provide them via the plugin’s WordPress.org support forum. I’ll do my best to reply in a timely fashion and help as best I can.

Unfortunately, I cannot provide guaranteed support, nor do I provide support via any other means.

Was this plugin useful useful to you? Consider giving it a rating. If you’re inclined to give it a poor rating, please first post to the support forum to give me a chance to address or explain the situation.

One reply on “Inject Query Posts”