Listing WordPress posts with a User Defined Tag
Posted: Sun May 16, 2010 11:08 am
I was messing around with attempts to list WordPress posts in my CMS-MS. I could do this through RSS feeds but they provided limited capabilities. So I created a few scripts in order to help with achieving this. Detailed instructions are available at this post. In short, you need to have a wp.php file with the code:
Then a User Defined Tag with the code:
And then use the tag in the following ways:
* List all posts with the tag xyz:
* List 10 posts with the tag xyz:
* List all posts with the tag code after May 1st, 2009:
Crude way of achieving the thing but hope it helps someone also in need of integrating WordPress.
Code: Select all
<?php
require_once('/path/to/wordpress/wp-blog-header.php');
// edit the path in the line above to point to your wp-blog-header.php
$tag = isset($_REQUEST['--tag']) ? $_REQUEST['--tag'] : 'code';
$count = isset($_REQUEST['--count']) ? $_REQUEST['--count'] : '-1';
$after = isset($_REQUEST['--after']) ? $_REQUEST['--after'] : '1970-01-01';
function filter_where($where = '') {
global $after;
$where .= " AND post_date >= '".$after."'";
return $where;
}
add_filter('posts_where', 'filter_where');
query_posts('tag='.$tag.'&posts_per_page='.$count);
echo "<ul>";
if ( have_posts() ) : while ( have_posts() ) : the_post();
echo "<li>";
echo "<a href=\"";
echo the_permalink();
echo "\">";
echo the_title();
echo "</a>";
echo " (";
echo the_time('F jS, Y');
echo ")";
echo "</li>\n";
endwhile; else:
echo "<li>No posts found</li>\n";
endif;
echo "</ul>";
wp_reset_query();
?>
Code: Select all
$path = 'whoami | php -q /path/to/wp.php';
// edit the path in the line above to point to the wp.php created in previous step
// whoami command piped for no reason because my script wasn't
// producing any output without it
if(isset($params['tag'])) {
$path .= ' --tag='.$params['tag'];
}
if(isset($params['count'])) {
$path .= ' --count='.$params['count'];
}
if(isset($params['after'])) {
$path .= ' --after='.$params['after'];
}
echo `$path`;
* List all posts with the tag xyz:
Code: Select all
{wp_posts_with_tag tag="xyz"}
Code: Select all
{wp_posts_with_tag tag="xyz" count="10"}
Code: Select all
{wp_posts_with_tag xyz="xyz" after="2009-05-01"}