Hijack The Loop, Drop in Plugin, Call with Short Code

What we are going to do

hijack ‘The Loop’
How create a wp_query object
modify loop
drop it into a plugin
call it with a shortcode passing parameters

This is classic information of how to pull certain posts onto certain pages using the best practices. First, The Loop will be covered, which is the powerhouse of your WordPress site. Next, WP_Query will be introduced, which is the cleanest way to pull information from your WordPress database. Then the code/query function will be setup with a short code in a plugin. Lastly, the short code will be provided that can be easily used to pull the posts of the desired tag (for this example).

Mitcho presenting The Loop @WCBoston 2010

These are notes from Mitcho’s presentation:

loop basics

post are called from the database
code loops through the posts to find the match

//if there are posts
if (have posts()) :
//while there are posts
*while (have_posts()) :
//get the post
//do stuff with it, calls the Template_Tags
*the_post(); //<-sets up the post
get sidebar();
get footer();

Every theme’s php files are built on this basic structure

The Loop is where you can use Template Tags

Where do get posts come from???

Every Loop has a query —template file and query based on your URL
/archive/123 -> single post
/archives -> archive (10 past posts)
/tag/chicken -> all chicken articles

Customizing the Loop

query controls what info is presented.

Custom queries

possible applications
*Create custom feed/displays
ephramzerb.com/projects/feed-wrangler/ //^custom feeds based on a query
*Pull information on other posts
from within the theme’s loop

Roll your own query

ex: pull information on other posts from the theme’s Loop
Display other posts with a specific criteria, like a tag
Wrap it up in a shortcode
The idea Create a new WP_Query object
given a $tag
new WP_Query(array(‘tag’=>$tag))
call/name it $my_query
Create s Loop for $my_query
Do stuff in it ‘display the title’
Make your own loop-make sure the loop controller are using
$my_query, not the default ($wp_query)

if ($my_query->have_posts()) :
while ($my_query->have_posts()) :

Andrew Nacin WP_Query

@ WC Portland 2011
*”The Loop”
Every query object has its own methods isauthor(), has conditional tags about what the query is actually trying to fetch

when loading a WP site, by the time we get to the theme, we have all the posts fetching all the post and then fetch them again; WP_Query all ready runs 4 more queries (fetch all the posts, figure out how many post total exists, all metadata, all metadata keys in one shot, all terms for all posts for all taxonomies queried in one shot) (query posts more queries) Better for performance.

/*can filter in the main query
paging will be handled*/

back to Mitcho-

The Code

The result (setting it up as a short code):

 add_shortcode('others', 'get_other_posts');
 function get_other_posts($attributes) {
   $return = '';
   $tag = $attributes['tag'];
   $my_query = new WP_Query(array('tag'=>$tag));
   if ( $my_query->have_posts() ) {
      $return .= "<ul>";
      while ( $my_query->have_posts() ) {
	$return .= "<li>";
	$return .= get_the_title();
	$return .= "</li>";
	$return .= "<li>";
	$return .= get_the_content();
	$return .= "</li>";
	$return .= "</ul>";
     return $return;

packaged in a plugin

short code to call posts tag ubiquity:
[others tag=ubiquity]

to pull posts tagged as Headlines:
[others tag=Headlines]


More about WP_Query


Filter every query

customize what information is displayed globally
hide all my tweets
(if to style them with a feed)
Use request filter to specify that we don’t want results from the “tweet” category #10 (have to use cat id)
The result (gets added to the bottom of the plugin file:

add_filter('request', 'request_filter');
function request_filter($request) {
  $request['cat'] = '-10'; //tweets are category 10
  return $request;

note meta

see it for where it’s defined

don’t forget to activate the plugin, once installed.








site files tested on

local environment http://whiterabbit.loc/?page_id=83
shortcode placed on pages
plugins activated
plugins added

category happenings

Leave a Reply

Your email address will not be published. Required fields are marked *