Page 1 of 1

Listing WordPress posts with a User Defined Tag

Posted: Sun May 16, 2010 11:08 am
by krkhan
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:

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();
?>
Then a User Defined Tag with the code:

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`;
And then use the tag in the following ways:

* List all posts with the tag xyz:

Code: Select all

{wp_posts_with_tag tag="xyz"}
* List 10 posts with the tag xyz:

Code: Select all

{wp_posts_with_tag tag="xyz" count="10"}
* List all posts with the tag code after May 1st, 2009:

Code: Select all

{wp_posts_with_tag xyz="xyz" after="2009-05-01"}
Crude way of achieving the thing but hope it helps someone also in need of integrating WordPress.

Re: Listing WordPress posts with a User Defined Tag

Posted: Sun May 16, 2010 5:54 pm
by Nullig
There is already a plugin for this:

http://dev.cmsmadesimple.org/projects/wp_blog_posts

Nullig

Re: Listing WordPress posts with a User Defined Tag

Posted: Sun May 16, 2010 5:58 pm
by krkhan
Thanks for the link, Nullig. The plugin does look nice and will definitely save lots of manual trouble. Still, from the plugin description:
NOTE: this plugin will only work if your Wordpress installation uses the same database as your CMSMS installation and your Wordpress tables use the standard "wp_" prefix.
There are no restrictions with the simple PHP code listed above since it uses whatever setting WP uses and does not depend upon CMSMS installation.