Inspirated

 
 

May 16, 2010

HOWTO: Query WordPress posts in CMS Made Simple

Filed under: Blog — krkhan @ 3:53 pm

While I run my blog at inspirated.com, I aggregate posts related to coding at the subdomain code.inspirated.com. The websites are run through WordPress and CMS Made Simple respectively.

For the latter, I needed to find a way of fetching blog posts from the main site for linking. Initially, I used tag feeds for this purpose. That is, I used the RSS module for CMS-MS and fetched the feed for a particular tag (e.g., inspirated.com/tag/code/feed). This worked well for a while until the feeds became large and I noticed that only the most recent 10 posts were showing up in the listings.

Digging around, I found this piece of documentation which explains how one can use custom queries for collecting WordPress posts from a blog. There was a catch however as the code for doing so could only be run globally. In other words, if I tried running the code mentioned on the page inside a User Defined Tag in CMS-MS I would get strange errors.

The solution was to run the code in a separate PHP file. Here’s how:

  1. Create a file named wp.php in your CMS-MS folder with the following code:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    
    <?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();
    ?>
  2. Add a User Defined Tag in CMS-MS with the following code:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    
    $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`;
  3. Use the tag in any CMS-MS page with any of the following combinations:
    • List all posts with the tag code:
      {wp_posts_with_tag tag="code"}
    • List 10 posts with the tag code:
      {wp_posts_with_tag tag="code" count="10"}
    • List all posts with the tag code after May 1st, 2009:
      {wp_posts_with_tag tag="code" after="2009-05-01"}

Custom queries are very powerful once you get them working. Anyone planning on using them should take a look at the function reference for getting to grips with the flexibility they offer.

Tags: , , , , ,

2 Comments

  1. F*cking neat trick with exec. :-)

    Comment by Petar Pavlovic — November 23, 2010 @ 4:16 pm

  2. Very nice solution, I was just about to do something similar when I gave Google a whirl. Glad I did!

    Comment by Matt — June 16, 2011 @ 12:14 am

RSS feed for comments on this post.

Sorry, the comment form is closed at this time.