Skip to content

Instantly share code, notes, and snippets.

@EdHurtig
Created July 14, 2014 20:14
Show Gist options
  • Save EdHurtig/2190c3f4ae96625326e5 to your computer and use it in GitHub Desktop.
Save EdHurtig/2190c3f4ae96625326e5 to your computer and use it in GitHub Desktop.
A very shorthand way of looping through all the posts in your blog that rids you of all the clutter of WP_Query and get_posts
<?php
/**
* Allows you to loop through a simple posts query
* @example foreach_post( function () { wp_delete_post( $post->ID ); } );
*
* @example foreach_post( 'faq', function () { echo $post->post_title . '<br />' } );
* @example foreach_post( array( 'book', 'faq'), function () { echo $post->post_title; } );
* @example foreach_post( 'book,faq', function () { echo $post->post_title; } );
*
* @example foreach_post( false, false, function () { echo $post->post_title; } );
* @example foreach_post( 'post', array('draft', 'publish'), function () { echo $post->post_title; } );
* @example foreach_post( 'post', 'draft,publish', function () { echo $post->post_title; } );
*
* @example foreach_post( 'post', 'publish', -1, function () { echo $post->post_title; } );
* @example foreach_post( 'post', 'publish', 5, function () { echo $post->post_title; } );
* @example foreach_post( 'post', 'publish', 10, function () { echo $post->post_title; } );
*
* @example foreach_post( array( 'book', 'faq'), function () { echo $post->post_title; } );
*
* @param $lambda
*
* @return array An Array of posts that were passed by the foreach loop
*/
function foreach_post( $arg0, $arg1 = null, $arg2 = null, $arg3 = null, $arg4 = null ) {
$query = array( 'posts_per_page' => - 1 );
$lambda = false;
if ( isset( $arg0 ) && ! is_callable( $arg0 ) ) { // If the first arg set and not a callable then it must be a post_type arg
if ( is_string( $arg0 ) ) {
$query['post_type'] = explode( ',', $arg0 );
} elseif ( is_array( $arg0 ) ) {
$query['post_type'] = $arg0;
} else {
// nothing
}
} else { // Otherwise it is a lambda and we will use the default query
$lambda = $arg0;
}
if ( isset( $arg1 ) && ! is_callable( $arg1 ) && ! $lambda ) { // If the second arg set, not a callable, and the last arg was not a callable then it must be a post_status arg
if ( is_string( $arg1 ) ) {
$query['post_status'] = explode( ',', $arg1 );
} elseif ( is_array( $arg1 ) ) {
$query['post_status'] = $arg1;
} else {
// nothing
}
} elseif ( isset( $arg1 ) && is_callable( $arg0 ) ) { // the last arg was a callable then this is an override query array
$query = array_merge( $query, $arg1 );
} elseif ( isset( $arg1 ) ) { // This is the callable
$lambda = $arg1;
}
if ( isset( $arg2 ) && ! is_callable( $arg2 ) && ! $lambda ) { // If the third arg set and not a callable and the last arg was not a lambda then this is a post_per_page override
if ( is_string( $arg2 ) || is_numeric( $arg2 ) ) {
$query['posts_per_page'] = intval( $arg2 );
} else {
// nothing
}
} elseif ( isset( $arg2 ) && is_callable( $arg1 ) ) { // the last arg was a callable then this is an override query array
$query = array_merge( $query, $arg2 );
} elseif ( isset( $arg2 ) ) { // Nope this is a lambda
$lambda = $arg2;
}
if ( isset( $arg3 ) && is_callable( $arg3 ) ) { // Well is there a fourth arg and was the third arg a callable... in that case then the fourth arg is an override query array
$lambda = $arg3;
} elseif ( isset( $arg3 ) && is_callable( $arg2 ) ) {
$query = array_merge( $query, $arg3 );
}
if ( isset( $arg4 ) && is_callable( $arg3 ) ) { // Well is there a fourth arg and was the third arg a callable... in that case then the fourth arg is an override query array
$query = array_merge( $query, $arg4 );
}
$returns = array();
// get the posts
$posts = get_posts( $query );;
// Loop through the results
foreach ( $posts as $post ) {
$returns[$post->ID] = $lambda( $post );
}
return $returns;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment