Last active
December 14, 2015 01:19
-
-
Save bonny/5005579 to your computer and use it in GitHub Desktop.
with_posts() WordPress function example usage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Examples for WordPress function with_posts() | |
* | |
* The main function is available here: | |
* https://gist.github.com/bonny/5011943 | |
* | |
* Author and contact info: | |
* https//twitter.com/eskapism | |
* | |
**/ | |
// Do something with a single post, by ID | |
with_posts(14824, function(){ | |
echo "<p>Got a single page with title " . get_the_title() . "</p>"; | |
}); | |
// Do something with many posts, by ID, in the order that they are in the string | |
with_posts("14831,987,1,235", function(){ | |
echo "<p>Got page with title: " . get_the_title() . "</p>"; | |
}); | |
// Do something with posts by slug | |
// Can be multiple posts, because multiple posts can have the same slug, | |
// but be in different places in the hierarchy | |
with_posts("about-us", function() { | |
echo "<br>Got post with title: " . get_the_title(); | |
}); | |
// Do something with multitple posts, by post slug | |
with_posts("about-us,our-software,copyrights", function() { | |
echo "<br>Got post with title: " . get_the_title(); | |
}); | |
// Do something with a post object | |
$post_id = 14831; | |
$post_obj = get_post($post_id); | |
with_posts($post_obj, function(){ | |
echo "<p>" . get_the_title() . "</p>"; | |
}); | |
// Do something with a wp_query-object | |
$wp_query_obj = new wp_query('post_type=regions&posts_per_page=3&orderby=title&order=asc'); | |
with_posts($wp_query_obj, function(){ | |
echo "<p>" . get_the_title() . "</p>"; | |
}); | |
// Do something with a wp_query-compatible string | |
$return_val = with_posts('post_type=regions&posts_per_page=5&orderby=title&order=desc', function() { | |
echo "<p>" . get_the_title() . "</p>"; | |
}); | |
// Do something with the currently requested post | |
$return_val = with_posts(get_queried_object(), function() { | |
echo "<p>" . get_the_title() . "</p>"; | |
}); | |
// With posts returns a wp_query object, with some extra information added. | |
// So you can continue using the generated wp_query. | |
// See examples below: | |
// Do something special if we didn't get any posts, | |
// like show message if query did not return posts | |
$return_val = with_posts('post_type=i_do_not_exist', function() { | |
// This code will never be executed, because no post of type "i_do_not_exist" exists | |
echo "<p>" . get_the_title() . "</p>"; | |
}); | |
if ($return_val->post_count === 0) { | |
echo "<p>The query did not return any posts.</p>"; | |
} | |
// With posts also accepts a third argument, if set to TRUE then | |
// the output is buffered and returned. | |
// See example below for usage: | |
// Only print headline if we got any posts | |
$return_val = with_posts('post_type=post', function() { | |
echo "<p>" . get_the_title() . "</p>"; | |
}, TRUE); | |
if ($return_val->post_count === 0) { | |
echo "<h2>I did NOT retrieve any posts</h2>"; | |
} else { | |
echo "<h2>I DID retrieve some posts</h2>"; | |
echo "<p>Output:</p>"; | |
echo $return_val->buffered_output; | |
} | |
// Only print headline if we got any posts | |
$return_val = with_posts('post_type=i_do_not_exist', function() { | |
// This code will never be executed | |
echo "<p>" . get_the_title() . "</p>"; | |
}, TRUE); | |
if ($return_val->post_count === 0) { | |
echo "<h2>I did NOT retrieve any posts</h2>"; | |
} else { | |
echo "<h2>I DID retrieve some posts</h2>"; | |
echo "<p>Output:</p>"; | |
echo $return_val->buffered_output; | |
} | |
// with posts passed two arguments to the function used as the second argument: | |
// $post and $arr_info | |
// $post = the current post that is beeing looped, | |
// so you quickly have access to things like $post->post_name, or whatever you need | |
// $arr_info = array with nice to have info, like post_count, current_post, post, and the full wp_query-query, | |
// so you have access to everything you would have access to in a regular wp_query-loop | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment