{"id":91188,"date":"2010-09-27T08:00:00","date_gmt":"2010-09-27T08:00:00","guid":{"rendered":"https:\/\/www.weixiaoduo.com\/plugins\/2024\/02\/22\/inject-query-posts\/"},"modified":"2025-04-23T14:37:00","modified_gmt":"2025-04-23T14:37:00","slug":"inject-query-posts","status":"publish","type":"post","link":"https:\/\/www.weixiaoduo.com\/plugins\/inject-query-posts\/","title":{"rendered":"Inject Query Posts"},"content":{"rendered":"<p>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.<\/p>\n<p>WordPress\u2019s template tags are intended to be used within \u2018the loop\u2019. 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\u2019t 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.<\/p>\n<p>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\u2019s variables.<\/p>\n<p>Example:<\/p>\n<pre><code><?php \/\/ Say we're in the sidebar\n\n\/\/ We've gotten some post objects on our own.\n$posts = c2c_get_random_posts( 5, '' );\n\n\/\/ Inject the posts\nc2c_inject_query_posts( $posts );\n\n\/\/ Now let's display them via template tags:\nif ( have_posts() ) :\n    while ( have_posts() ) : the_post(); ?>\n\n        <li><a href=\"<?php the_permalink() ?>\" rel=\"bookmark\" title=\"Permanent Link to <?php the_title_attribute(); ?>\"><?php the_title(); ?><\/a><\/li>\n\n    <?php endwhile;?>\n<?php endif; ?>\n<\/code><\/pre>\n<p>Links: <a href=\"https:\/\/coffee2code.com\/wp-plugins\/inject-query-posts\/\" rel=\"nofollow ugc\">Plugin Homepage<\/a> | <a href=\"https:\/\/wordpress.org\/plugins\/inject-query-posts\/\" rel=\"ugc\">Plugin Directory Page<\/a> | <a href=\"https:\/\/github.com\/coffee2code\/inject-query-posts\/\" rel=\"nofollow ugc\">GitHub<\/a> | <a href=\"https:\/\/coffee2code.com\" rel=\"nofollow ugc\">Author Homepage<\/a><\/p>\n<h3>Developer Documentation<\/h3>\n<p>Developer documentation can be found in <a href=\"https:\/\/github.com\/coffee2code\/inject-query-posts\/blob\/master\/DEVELOPER-DOCS.md\" rel=\"nofollow ugc\">DEVELOPER-DOCS.md<\/a>. That documentation covers the template tag and hooks provided by the plugin.<\/p>\n<p>As an overview, this is the template tag provided by the plugin:<\/p>\n<ul>\n<li><code>c2c_inject_query_posts()<\/code> : Template tag to inject an array of posts into a query object as if that query object had obtained those posts via a query.<\/li>\n<\/ul>\n<p>These are the hooks provided by the plugin:<\/p>\n<ul>\n<li><code>inject_query_posts_preserve_query_obj<\/code> : Overrides the value of the <code>$preserve_query_obj<\/code> argument passed to the function. This is not typical usage for most users.<\/li>\n<li><code>c2c_inject_query_posts<\/code> : Allows use of an alternative approach to safely invoke <code>c2c_inject_query_posts()<\/code> in such a way that if the plugin were deactivated or deleted, then your calls to the function won\u2019t cause errors in your site.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Facilitates injecting an array of posts into a WP query object as if queried. Particularly useful to allow use of standard template tags.<\/p>\n","protected":false},"author":65,"featured_media":293480,"comment_status":"open","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"slim_seo":{"title":"Inject Query Posts - \u8587\u6653\u6735\u63d2\u4ef6\u76ee\u5f55","description":"Facilitates injecting an array of posts into a WP query object as if queried. Particularly useful to allow use of standard template tags."},"footnotes":""},"categories":[1],"tags":[198,542,1361,14746,18574],"class_list":["post-91188","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized","tag-loop","tag-posts","tag-query","tag-template-tags","tag-wp_query"],"_links":{"self":[{"href":"https:\/\/www.weixiaoduo.com\/plugins\/wp-json\/wp\/v2\/posts\/91188","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.weixiaoduo.com\/plugins\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.weixiaoduo.com\/plugins\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.weixiaoduo.com\/plugins\/wp-json\/wp\/v2\/users\/65"}],"replies":[{"embeddable":true,"href":"https:\/\/www.weixiaoduo.com\/plugins\/wp-json\/wp\/v2\/comments?post=91188"}],"version-history":[{"count":0,"href":"https:\/\/www.weixiaoduo.com\/plugins\/wp-json\/wp\/v2\/posts\/91188\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.weixiaoduo.com\/plugins\/wp-json\/wp\/v2\/media\/293480"}],"wp:attachment":[{"href":"https:\/\/www.weixiaoduo.com\/plugins\/wp-json\/wp\/v2\/media?parent=91188"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.weixiaoduo.com\/plugins\/wp-json\/wp\/v2\/categories?post=91188"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.weixiaoduo.com\/plugins\/wp-json\/wp\/v2\/tags?post=91188"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}