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
*the_content();
*endwhile;
endif;
get sidebar();
get footer();

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

The Loop is where you can use Template Tags
codex.wordpress.org/Template_Tags

Where do get posts come from???

Every Loop has a query —template file and query based on your URL
codex.wordpress.org/Template_Hierarchy
/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()) :
$my_query->the_post();
the_content();
endwhile;
endif;

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() ) {
	$my_query->the_post();
	$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]

codex.wordpress.org/Template_Tags/query_posts

More about WP_Query

http://codex.wordpress.org/Class_Reference/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
core.trac.wordpress.org

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

references

http://codex.wordpress.org/The_Loop
http://codex.wordpress.org/Class_Reference/WP_Query
http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters
http://codex.wordpress.org/Class_Reference/WP_Query#Parameters
****http://codex.wordpress.org/Function_Reference

http://wp.smashingmagazine.com/2013/01/14/using-wp_query-wordpress/

http://codex.wordpress.org/Post_Type_Templates#Displaying_Custom_Taxonomies
http://codex.wordpress.org/Shortcode_API
http://wordpress.org/support/topic/custom-query-for-category-and-tag
http://www.wprecipes.com/wordpress-function-get-category-id-using-category-name
http://wordpress.org/support/topic/query-posts-then-put-into-array

http://codex.wordpress.org/Posts
http://codex.wordpress.org/Post_Thumbnails

http://core.trac.wordpress.org/browser/tags/3.5.1/wp-includes/post-template.php#L0

http://wp.smashingmagazine.com/2012/11/08/complete-guide-custom-post-types/

site files tested on

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

http://peaceoftheocean.com/WordPressNotes
category happenings

Leave a Reply

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

*

*