Title: Widget commands
Last modified: August 19, 2016

---

# Widget commands

 *  Resolved [blogger1234](https://wordpress.org/support/users/blogger1234/)
 * (@blogger1234)
 * [16 years, 2 months ago](https://wordpress.org/support/topic/widget-commands/)
 * I have been tweaking a theme that I like, but I have come to a point that I need
   someone’s help. Currently, my sidebar is custom made by the original designer
   and shows three boxes: tags, recent comments, and “Popular” posts (popular is
   defined by how many comments are on a given post).
 * I want to change the “Popular” box to show the most recent posts published in
   order. In the theme editor, the sidebar file has a div id – “Popular”, which 
   I believe is getting instructions from the themes function file. From the themes
   function file, I found this:
 * >  <?php
   >  function popularPosts($num) { global $wpdb;
   >  $posts = $wpdb->get_results(“SELECT comment_count, ID, post_title FROM $wpdb-
   > >posts ORDER BY comment_count DESC LIMIT 0 , $num”);
   >  foreach ($posts as $post) {
   >  setup_postdata($post); $id = $post->ID; $title
   > = $post->post_title; $count = $post->comment_count;
   >  if ($count != 0) {
   >  $popular .= ‘
   >  - ‘;
   >     $popular .= ‘[‘ . $title . ‘](https://wordpress.org/support/topic/widget-commands/&apos; . get_permalink($id) . &apos;?output_format=md)‘;
   >    $popular .= ‘
   >  - ‘;
   >     } } return $popular;
 * Can anyone tell me what I need to change to make this function collect and display
   the most recent posts? Thanks!

Viewing 11 replies - 1 through 11 (of 11 total)

 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [16 years, 2 months ago](https://wordpress.org/support/topic/widget-commands/#post-1408050)
 * This is UNTESTED, but should be close:
 *     ```
       <?php
       function popularPosts($num) {
       global $wpdb;
   
       $posts = $wpdb->get_results("SELECT ID, post_title, post_date FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' AND post_date <= NOW() ORDER BY post_date DESC LIMIT 0 , $num");
   
       foreach ($posts as $post) {
       setup_postdata($post);
       $id = $post->ID;
       $title = $post->post_title;
   
       $popular .= ' 
   
       ';
       $popular .= '' . $title . ' ';
       $popular .= '
       ';
       }
       return $popular;
       ```
   
 * It will need some modification if you want to make the titles a link to the post.
 *  Thread Starter [blogger1234](https://wordpress.org/support/users/blogger1234/)
 * (@blogger1234)
 * [16 years, 2 months ago](https://wordpress.org/support/topic/widget-commands/#post-1408086)
 * Unfortunately, I get a:
 * > Parse error: syntax error, unexpected T_STRING in /home/content/17/5686317/
   > html/wp-content/themes/aparatus/functions.php on line 244
 * When I try that. Any other ideas? Thanks again for providing your thoughts.
 * Tyler
 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [16 years, 2 months ago](https://wordpress.org/support/topic/widget-commands/#post-1408107)
 * You posted a partial listing of the popularPosts function. Did you replace the
   part you posted with what I posted, or did you replace the whole function?
 *  Thread Starter [blogger1234](https://wordpress.org/support/users/blogger1234/)
 * (@blogger1234)
 * [16 years, 2 months ago](https://wordpress.org/support/topic/widget-commands/#post-1408333)
 * Probably only part… it’s tricky because it is part of a much larger function.
   The created (who is rather busy) of the theme suggested this, but it doesn’t 
   work either:
 * > I wouldn’t mess with the popularPosts function – since that was built for specifically
   > that. If you want to pull recent posts – go in the tabbed-container.php and
   > take out
    - <?php echo popularPosts(10); ?>
 * replace with:
    - <?php global $post; $myposts = get_posts(‘numberposts=10′); foreach($myposts
      as $post) : setup_postdata($post); ?>
    - 
    -  <?php endforeach; ?>
 * He is talking about putting it here:
 * > <div id=”tabbed-container”>
   >  <div id=”myTabs” class=”mootabs”>
   >  <ul class=”mootabs_title”> <li title=”Popular”
   > >Popular <li title=”RecentComments”>Recent Comments <li title=”Tags”>Tags
   >  <div id=”Popular” class=”mootabs_panel”>
    -  <?php echo popularPosts(10); ?>
 *  </div><!–popular–>
 *  <div id=”RecentComments” class=”mootabs_panel”>
 *  <?php my_rec_comments(7); ?>
 *  </div>
    <div id=”Tags” class=”mootabs_panel”>
 *  <?php wp_tag_cloud(); ?>
    </div> </div> </div><!–tabbed-container–>
 * If its useful to you, the site I’m working on is: [http://seattlemiscellany.com/](http://seattlemiscellany.com/)
 * And it is the “Popular” tab on the right I want to be most recent. Thanks again
   for following up with me.
 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [16 years, 2 months ago](https://wordpress.org/support/topic/widget-commands/#post-1408362)
 * Looks like your developer’s suggestion is correct. Did you get it fixed?
 *  Thread Starter [blogger1234](https://wordpress.org/support/users/blogger1234/)
 * (@blogger1234)
 * [16 years, 2 months ago](https://wordpress.org/support/topic/widget-commands/#post-1408369)
 * Unfortunately not. I get a
 * > Parse error: syntax error, unexpected ‘=’ in /home/content/17/5686317/html/
   > wp-content/themes/aparatus/tabbed-container.php on line 12
 * message then preview box stops working.
 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [16 years, 2 months ago](https://wordpress.org/support/topic/widget-commands/#post-1408372)
 * Why don’t you paste the entire tabbed-container.php in [wordpress.pastebin.ca](http://wordpress.pastebin.ca/),
   and post the link to it here so we can see all of the code.
 *  Thread Starter [blogger1234](https://wordpress.org/support/users/blogger1234/)
 * (@blogger1234)
 * [16 years, 2 months ago](https://wordpress.org/support/topic/widget-commands/#post-1408376)
 * With the suggested change: [http://wordpress.pastebin.ca/1821597](http://wordpress.pastebin.ca/1821597)
 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [16 years, 2 months ago](https://wordpress.org/support/topic/widget-commands/#post-1408377)
 * OK, two little problems:
    1. There is an extra opening `<ul>` right after `<div id="Popular" class="mootabs_panel"
       >`. Just delete it.
    2. The first tick mark after get_posts is not an apostrophe, it is a backtick. 
       Change to an apostrophe.
 * That should fix it.
 *  Thread Starter [blogger1234](https://wordpress.org/support/users/blogger1234/)
 * (@blogger1234)
 * [16 years, 2 months ago](https://wordpress.org/support/topic/widget-commands/#post-1408378)
 * That did the trick! Thanks again for your help, I really appreciate it. It’s 
   been a long time since I’ve been comfortable with HTML and all its changes.
 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [16 years, 2 months ago](https://wordpress.org/support/topic/widget-commands/#post-1408384)
 * Glad it worked! Now, please use the dropdown at top right to mark this topic ‘
   Resolved’.

Viewing 11 replies - 1 through 11 (of 11 total)

The topic ‘Widget commands’ is closed to new replies.

## Tags

 * [posts](https://wordpress.org/support/topic-tag/posts/)
 * [recent](https://wordpress.org/support/topic-tag/recent/)
 * [sidebar](https://wordpress.org/support/topic-tag/sidebar/)
 * [theme function](https://wordpress.org/support/topic-tag/theme-function/)
 * [widget](https://wordpress.org/support/topic-tag/widget/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 11 replies
 * 2 participants
 * Last reply from: [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * Last activity: [16 years, 2 months ago](https://wordpress.org/support/topic/widget-commands/#post-1408384)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
