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.
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 " - ";
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:
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`";
}
I would use wp_get_recent_posts and pass in the arguments for category.
Final code:
This code is another way of getting the recently updated posts and pages.