Customizable Post Listings
- Author:Scott Reilly
- Version:3.0.1
- Last update:03 April 2008
- Compatibility:WP 2.3+ and 2.5
- Download:[ zip ]
- Description:
Display Recent Posts, Recently Commented Posts, Recently Modified Posts, Random Posts, and other post, page, or draft listings using the post information of your choosing in an easily customizable manner. You can narrow post searches by specifying categories and/or authors, among other things.
-
Special Upgrade Note
NOTE: Users of any older version of Customizable Post Listings (versions earlier than v3.0) will want to review their usage and compare it to the new arguments for the various functions. You may have to change the way in which you call the functions! If no posts appear, or the posts that appear are not the posts you were expecting, this is probably the reason why…
Percent-substitution tags
These are the various percent-substitution tags available to you for use in the
$formatargument of the template functions.These are the pseudo-function percent-substitution tags. They act like functions, so you can send them data to work with. You can even send percent-substutition tags which will get evaluated before the function gets them.
- %post_custom(field,format,none)%
-
A lite version of the Get Custom Field Values plugin to display custom fields.
- Arguments:
- field = a custom field key value
- format = an optional percent-substitution format string for the output. In additional to all the existing percent-substitution tags, two additional tags are also available for use here:
- %field% : the value you provided as “field”
- %value% : the value of the post’s custom field; if the post has more than one custom field matching “field” then all their values are joined with a comma
If not specified, then the format defaults to simply
%value%, displaying the custom field’s value. - none = text to display if the post does not have the custom field (leave blank to show nothing).
- Examples:
%post_custom(quantity_in_stock)%%post_custom(mood, Today I am feeling %value%)%%post_custom(Books Read, Today I read: %value%, nothing)%
- %post_date(date_format)%
-
Displays the post’s publication date using a custom date format, overriding the configured default format.
- Argument:
- date_format = a PHP date-format string. e.g.
%post_date(F d, Y)%
See http://us2.php.net/date for more info on date format strings.
- Examples:
%post_date(Y-m-d)%%post_date(M n, Y)%
- date_format = a PHP date-format string. e.g.
- %post_other(other_field)%
-
Retrieve the value of any post table field. This is useful for accessing a field that does not already have a percent-substitution tag counterpart (generally for fields added to the posts table by another plugin or for when WordPress updates the posts table to include something this plugin hasn’t been updated to handle yet), or if you need to programmatically determine what field you want during runtime.
- Argument:
- other_field = the name of a field in the posts table
- Examples:
%post_other(post_rating)%%post_other(menu_order)%
- %last_commenters(limit,more,between)%
-
Lists the most recent commenters to the post.
- Arguments:
- limit = number of latest commenters to list by name
- more = text to show after listed commenter if there are more commenters to the post; default is […]
- between = text to show between listed commenters; default is “, “
- Examples:
%last_commenters(5, and others!)%%last_commenters(1,,)%
- %function(function_name,arg1,arg2,…)%
-
Calls an arbitrary function.
- Arguments:
- function_name = name of the function to be called
- Additional arguments are optional and are passed to the function specified. Arguments caninclude percent-substitution tags, e.g.
%function(my_excerpt_maker,%post_content%)%
- Examples:
%function(strtoupper,%post_title%)%%function(my_excerpt_maker,%post_content%)%
NOTE: Currently you cannot embed a pseudo-function percent-substitution within another. This is invalid:
%function(mood_handler,%post_custom(mood)%)%
-
Screenshots
Various screenshots of the plugin’s admin options page (click to enlarge):
-
Installation
- Download the file customizable-post-listings.zip and unzip it into your wp-content/plugins/ directory.
- Activate the plugin through the ‘Plugins’ admin menu in WordPress
- Go to the Options → CPL (or in WP 2.5+: Settings → CPL) admin options page. Optionally customize the settings. You can read documentation on the template tags, arguments, and percent-substitution tags there as well.
- In your sidebar.php (or other template file), insert calls to post listings function(s) provided by the plugin.
-
Template Functions
These are the template functions made available by this plugins.
c2c_get_recent_posts()provides the core
functionality. The other template functions are added as a convenience to provide different default argument values for their
different purposes.- Get recent posts:
function c2c_get_recent_posts( $num_posts = 5, $format = "<li>%post_date%: %post_URL%</li>", $categories = '', $orderby = 'date', $order = 'DESC', $offset = 0, $date_format = 'm/d/Y', $authors = '', $post_type = 'post', $post_status = 'publish', $nclude_passworded_posts = false, $extra_sql_where_clause = '' ) - Get Random Posts
function c2c_get_random_posts( $num_posts = 5, $format = "<li>%post_date%: %post_URL%</li>", $categories = '', $order = 'DESC', $offset = 0, $date_format = 'm/d/Y', $authors = '', $post_type = 'post', $post_status = 'publish', $include_passworded_posts = false ) - Get Recently Commented Posts
function c2c_get_recently_commented( $num_posts = 5, $format = "<li>%comments_URL%<br />%last_comment_date%<br />%comments_fancy%</li>", $categories = '', $order = 'DESC', $offset = 0, $date_format = 'm/d/Y h:i a', $authors = '', $post_type = 'post', $post_status = 'publish', $include_passworded_posts = false ) - Get Recently Modified Posts
function c2c_get_recently_modified( $num_posts = 5, $format = "<li>%post_URL%<br />Updated: %post_modified%</li>", $categories = '', $order = 'DESC', $offset = 0, $date_format = 'm/d/Y', $authors = '', $post_type = 'post', $post_status = 'publish', $include_passworded_posts = false )
Arguments:
- $num_posts
- The number of posts to list.
- Default:
5 - $format
- String containing percent-substitution tags and text to be displayed for each post retrieved.
- $categories
- Optional. Space-separated list of category IDs — leave empty to search for posts regardless of category.
- Default value:
'' - $orderby
- Optional. The post field to be used as the sort field. Except for a few exceptions, the value provided will assume that
post_will be prepended to it. So a value of'title'will sort the posts by the
'post_title'field. - Possible values: any post table field that begins with
post_. In practice, it’ll likely be one of the following:'ID', 'date', 'modified', 'name', 'title'. The exceptions to this are:rand(): to randomize the order of postsmax_comment_date: to order by the date of the most recent comment for each postSQL:$sql_here: if you’re feeling advanced, you can directly specify the SQL for the ORDER BY clause by prependingSQL:to the $orderby value
- Default value:
'date' - $order
- Optional. Indicates whether the posts should be listed in ascending or descending order.
- Possible values:
'ASC'or'DESC' - Default value:
'DESC' - $offset
- Optional. Number of posts to skip from the beginning of the list of posts found.
- Default value:
0 - $date_format
- Optional. Date format, PHP-style, to use to format any dates. If set to
'', then the blog default date format of
'$default_date'will be used. Keep in mind that the template functions provide their own date format. - Possible values: see http://us2.php.net/date for more info on date format strings.
- Default value:
'm/d/Y' - $authors
- Optional. Space-separated list of author IDs — leave empty to search for posts regardless of author.
- Default value:
'' - $post_type
- Optional. Space-separated list of post_type values to consider in the search.
- Possible values:
post, page, attachment - Default value:
'post' - $post_status
- Optional. Space-separated list of post_status values to consider in the search.
- Possible values:
publish, draft, private, pending, and/or future - Default value:
'publish' - $include_passworded_posts
- Optional. Should passworded posts be included in the serach?
- Possible values:
trueorfalse - Default value:
false - $extra_sql_where_clause
- Optional. Additional SQL to be added to the query’s WHERE clause, to facilitate refining the post search in a manner not achievable with existing arguments.
- Default value:
''
- Get recent posts:
-
Tips & Examples
The “Recent” listings on the sidebar of this site all use this plugin.The simplest examples would be the following (for brevity’s sake, in the examples I’m going to specify 3 as the number of posts I want returned):
<ul> Recent Posts <?php c2c_get_recent_posts(3); ?> </ul>
Would output something like:
- Recent Posts
- 08/03/2004: Patch: Dealing with multiple <!–more–>
- 08/03/2004: Patch: Balancing pre-’more’ tags
- 08/03/2004: Patch: Fixing balanceTags()
<ul>Recently Commented <?php c2c_get_recently_commented(3); ?> </ul>
Would output something like:
- Recently Commented
- Plugin: Category Image(s)
08/23/2004 10:25 am
14 Comments - Plugin: Get Custom Field Values
08/22/2004 04:35 pm
18 Comments - Plugin: wpPHPMailer
08/16/2004 01:15 am
11 Comments
<ul>Recently Updated <?php c2c_get_recently_modified(3); ?> </ul>
Would output something like:
- Recently Updated
- Plugin: Get Custom Field Values
Updated: 08/22/2004 - Plugin: wpPHPMailer
Updated: 08/17/2004 - Plugin: Hide or Cut Post Text
Updated: 08/17/2004
But what you may really want is to run the Recent Posts/Recently Commented Posts/Recently Updated Posts/Random Posts calls and customize the listing output. That’s where the percent substitution tags come into play. The second argument to each of the three functions above is the $format argument. $format is a string that represents how each post found by the function should be output. Any percent tags (as listed and defined above) will be replaced with the appropriate data.
So, let’s say you want a list of recent posts, to include the post author’s name linked to the archive of all of that author’s posts, plus a short excerpt of the post. And let’s say you only want posts from category ‘34′. You’d do this:
<ul> <?php c2c_get_recent_posts(3, "<li>%post_date%: %post_URL%<br />by: %post_author_posts%<br />%post_excerpt_short%</li>", "34"); ?> </ul>
Which would output something like:
- 07/25/2004: Plugin: Hide or Cut Post Text
by: Scott
I’m pleased to announce the release… - 07/08/2004: Plugin: Random File
by: Scott
I just finished the final touches… - 07/08/2004: Plugin: Auto-hyperlink URLs
by: Scott
Finally, I managed to tackle a…
-
Release Log
- 03 Apr 2008 : v3.0.1 - bugfix for SQL construction
- 31 Mar 2008 : v3.0 — Tons of updates! Compatible with WP 2.3+ and 2.5. Follow link for full update information.
- NOTE: Versions prior to v3.0 were documented here
- 27 Sep 2004 : v1.2 –
- Added the ability to get random posts, and added the convenience function
c2c_get_random_posts()to get them - Functions will now consider sticky posts (those in which ‘post_status’ gets set to ’sticky’) in addition to published posts
- Changed license from BSD-new to MIT
- Added the ability to get random posts, and added the convenience function
- 05 Sep 2004 : v1.02 — Bugfix to immediately return if no posts match criteria
- 03 Sep 2004 : v1.01 — Bugfix for when
$authorshas more than one author - 27 Aug 2004 : v1.0 — Released to the public
-
Copyright & Disclaimer
Copyright © 2004-2008 by Scott Reilly (aka coffee2code)Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-
Acknowledgements
Thanks to all those who have contributed feedback and support!


