Skip to content

Instantly share code, notes, and snippets.

@marteinn
Created November 6, 2010 16:27
Show Gist options
  • Save marteinn/665521 to your computer and use it in GitHub Desktop.
Save marteinn/665521 to your computer and use it in GitHub Desktop.
Manipulating wordpress the_loop - merging a original query with external posts.
<?php
// Run original query, exclude category (in our case 4)
query_posts( "showposts=40&cat=-4");
$wp_query->get_posts(); // this will force the query to run
// Grab different data, in our case, data from our twitter category
$twitter = get_posts( "cat=4&showposts=3");
// Then we insert posts into $wp_query->posts using a loop and splice
$step = floor( count($wp_query->get_posts())/count($twitter) );
for( $i=0; $i<count($twitter); $i++ )
{
array_splice( $wp_query->posts, $step*$i, 0, Array($twitter[$i]));
}
// And finally, we need to set a new length for the query
$wp_query->post_count = count($wp_query->posts);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment