Display Recently Updated Posts WordPress

Floating around the Internet is a piece of PHP code that is designed to display recently updated posts/pages in WordPress. After submitting a request to experts-exchange for assistance, within moments, I had the raw code I needed.

However, the original PHP code still didn’t work for my WordPress installation. After some tweaks and a lot of additional bells and whistles, I had managed to piece together the code needed to only display recently updated posts.

Read More

At this point I ask for help to show only posts from a specific category. Is it possible to show only posts from a specific category? Can you help?

this is the current code:

<div class="statistics">
<?php
$today = current_time('mysql', 1);
$howMany = 10; //Number of posts you want to display
if ($recentposts = $wpdb->get_results("SELECT ID, post_title, post_modified FROM $wpdb->posts    WHERE post_status = 'publish' AND post_type = 'post' AND post_name NOT LIKE  '%revision%' AND post_name NOT LIKE '%autosave%' AND post_modified_gmt < '$today' ORDER BY post_modified_gmt DESC LIMIT $howMany")) :
?>
<h2><?php _e('ULTIME SERIE TV AGGIORNATE'); ?></h2>         
<ul>
<?php
foreach($recentposts as $post) {
if ($post->post_title == '') {
$post->post_title = sprintf(__('Post #%s'), $post->ID);
}
/* If no post title exists one will be assigned to it. */
echo "<li><a href='".get_permalink($post->ID)."'>";
echo mysql2date('d/m/Y', $post->post_modified); 
echo "&nbsp;-&nbsp;";
echo $post->post_title;
echo '</a></li>';
}
?>
</ul>
<?php endif; ?>
</div>

The table are the categories stored in wp_term_taxonomy and which method is used to store items under a specific category database and its ID (term_taxonomy_id) This is a picture:

TABLE picture

In a plugin (recently-updated-posts) I found this function but I do not know how to put it in my code…

if ($options['excludeCategory']) {
$select .= ", GROUP_CONCAT(`tt`.`term_id`) AS `terms`";
$from   .= " LEFT JOIN `{$wpdb->term_relationships}` AS `tr` ON `tr`.`object_id` = `p`.`ID`"
                     . " LEFT JOIN `{$wpdb->term_taxonomy}` AS `tt` ON `tt`.`term_taxonomy_id` =   `tr`.`term_taxonomy_id`";
 $where  .= " AND `tt`.`taxonomy` = 'category'"
 . " AND `tt`.`term_id` NOT IN ({$options['excludeCategory']})";
 $group  = "GROUP BY `ID`";
 }

Related posts

Leave a Reply

3 comments

  1. I would use wp_get_recent_posts and pass in the arguments for category.

    <ul>
    <?php
    $args = array( 'category' => '5' );
    $recent_posts = wp_get_recent_posts( $args );
    foreach( $recent_posts as $post ){
    if ($post["post_title"] == '') {
    $post["post_title"] = sprintf(__('Post #%s'), $post["ID"]);
    }
    ?>
    <li><a href='<?php echo get_permalink($post["ID"]) ?>'>
    <?php echo mysql2date('d/m/Y', $post["post_modified"])?>
    &nbsp;-&nbsp;
    <?php echo $post["post_title"]?>
    </a></li>
    }
    ?>
    </ul>
    
  2. Final code:

    <div class="update">
    <?php
    $today = current_time('mysql', 1);
    if ($recentposts = $wpdb->get_results("SELECT ID, post_title, post_modified FROM $wpdb->posts    WHERE post_status = 'publish' AND post_type = 'post' AND post_name NOT LIKE  '%revision%' AND post_name NOT LIKE '%autosave%' AND post_modified_gmt < '$today' ORDER BY post_modified_gmt")) :
    ?>
    <h2><?php _e('GLI ULTIMI EPISODI AGGIUNTI'); ?></h2>        
    <ul>
    <?php $args = array('category' => '30', 'orderby'  => 'modified', 'post_status' => 'publish',   'posts_per_page' => 11);
    $recent_posts = wp_get_recent_posts($args);
    foreach ($recent_posts as $post) {
    if ($post["post_title"] == ''){ $post["post_title"] = sprintf(__('Post #%s'), $post["ID"]);
    }
    ?>
    <li><?php echo mysql2date('d/m/Y', $post["post_modified"]) ?>
    &nbsp;-&nbsp;
    <a href='<?php echo get_permalink($post["ID"]) ?>'>
    <?php echo $post["post_title"] ?>
    </a></li> <?php } ?>
    </ul>
    </div>
    <?php endif; ?>
    
  3. This code is another way of getting the recently updated posts and pages.

    <?php 
    
    $today  = current_time('mysql', 1);
    $number = 5; // number of posts
    
    if($recentposts = $wpdb->get_results("SELECT ID, post_title FROM $wpdb->posts WHERE post_status = 'publish' AND post_modified_gmt < '$today' ORDER BY post_modified_gmt DESC LIMIT $number")):
    
    ?>
    
    <h2><?php _e("Recently Updated"); ?></h2>
    <ul>
    <?php
    
    foreach($recentposts as $post) {
    
        if($post->post_title == '') $post->post_title = sprintf(__('Post #%s'), $post->ID);
        echo '<li><a href="'.get_permalink($post->ID).'">'.the_title().'</a></li>';
    
    } ?>
    </ul>
    
    <?php endif; ?>