Skip to content

Commit 66082ae

Browse files
author
infinitnet
committed
Initial release
1 parent 8580900 commit 66082ae

File tree

2 files changed

+139
-1
lines changed

2 files changed

+139
-1
lines changed

README.md

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,31 @@
1-
# relevant-recent-posts
1+
# Relevant Recent Posts
22
Adds WordPress shortcodes for recent posts lists.
3+
4+
## Purpose
5+
The Relevant Recent Posts plugin enhances your WordPress site by providing a simple yet flexible way to display lists of recent posts. It's particularly useful for bloggers and content-focused websites, where showcasing related or recent content can improve SEO, user engagement, and site navigation.
6+
7+
## Installation
8+
1. Download the latest release.
9+
2. Upload and install the plugin zip file under Plugins > Add New Plugin > Upload Plugin.
10+
3. Activate the plugin.
11+
12+
## Available Shortcodes
13+
- **[recentposts]**: Displays the 10 most recent posts from the same category as the current post.
14+
- **[recentposts count=5]**: Changes the number of displayed posts. Replace `5` with your desired number.
15+
- **[recentposts scope=global]**: Displays recent posts from all categories.
16+
- **[recentposts date=modified]**: Sorts posts by the last modified date instead of the published date.
17+
- **[recentposts class=example-class]**: Applies a custom CSS class (`example-class`) to the list for styling. Replace `example-class` with your own class name.
18+
- **[recentposts nonav]**: Removes `<nav>` tags from the posts list.
19+
- You can combine multiple parameters, like `[recentposts date=modified scope=global count=5 class=example-class]`.
20+
21+
## Usage
22+
Add the shortcode `[recentposts]` to any post, page, or widget where you want to display the list of recent posts. Customize its behavior using the available parameters.
23+
24+
## Support
25+
For support, feature requests, or bug reports, please visit the [GitHub issues page](https://github.com/infinitnet/relevant-recent-posts/issues).
26+
27+
## Contributions
28+
Contributions to the plugin are welcome. Please fork the GitHub repository and submit a pull request.
29+
30+
## Changelog
31+
- **1.0**: Initial release.

relevant-recent-posts.php

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php
2+
/**
3+
* Plugin Name: Relevant Recent Posts
4+
* Description: Adds shortcodes for recent posts lists.
5+
* Author: Infinitnet
6+
* Author URI: https://infinitnet.io/
7+
* Plugin URI: https://github.com/infinitnet/relevant-recent-posts
8+
* Update URI: https://github.com/infinitnet/relevant-recent-posts
9+
* Version: 1.0
10+
* License: GPLv3
11+
* Text Domain: relevant-recent-posts
12+
*/
13+
14+
if ( ! function_exists( 'infinitnet_relevant_recent_posts_shortcode' ) ) {
15+
16+
function infinitnet_relevant_recent_posts_shortcode($atts) {
17+
global $post;
18+
19+
$atts = shortcode_atts(array(
20+
'count' => 10,
21+
'scope' => 'category',
22+
'date' => 'published',
23+
'class' => '',
24+
'nonav' => false
25+
), $atts, 'recentposts');
26+
27+
$count = intval($atts['count']);
28+
$count = max(1, min($count, 200));
29+
30+
$valid_scopes = ['category', 'global'];
31+
if (!in_array($atts['scope'], $valid_scopes)) {
32+
$atts['scope'] = 'category';
33+
}
34+
35+
$valid_dates = ['published', 'modified'];
36+
if (!in_array($atts['date'], $valid_dates)) {
37+
$atts['date'] = 'published';
38+
}
39+
40+
if (!isset($post) || $post == null) {
41+
return '<p>' . esc_html__('Recent posts cannot be displayed.', 'relevant-recent-posts') . '</p>';
42+
}
43+
44+
$args = array(
45+
'posts_per_page' => $count,
46+
'orderby' => ($atts['date'] === 'modified') ? 'modified' : 'date',
47+
'order' => 'DESC'
48+
);
49+
50+
if ($atts['scope'] === 'category') {
51+
$categories = get_the_category($post->ID);
52+
if (!empty($categories)) {
53+
$category_ids = array_map(function($category) {
54+
return $category->term_id;
55+
}, $categories);
56+
$args['category__in'] = $category_ids;
57+
}
58+
}
59+
60+
$post_id = isset($post) ? $post->ID : 0;
61+
$scope = $atts['scope'];
62+
$date = $atts['date'];
63+
64+
$cache_key = "rrp_{$post_id}_{$scope}_{$date}";
65+
66+
$output = get_transient($cache_key);
67+
68+
if ($output === false) {
69+
$recent_posts_query = new WP_Query($args);
70+
71+
if ($recent_posts_query->have_posts()) {
72+
$class_attr = !empty($atts['class']) ? ' class="' . esc_attr($atts['class']) . '"' : '';
73+
74+
$output = $atts['nonav'] ? '' : '<nav>';
75+
$output .= '<ul' . $class_attr . '>';
76+
77+
while ($recent_posts_query->have_posts()) {
78+
$recent_posts_query->the_post();
79+
$output .= '<li><a href="' . esc_url(get_permalink()) . '">' . esc_html(get_the_title()) . '</a></li>';
80+
}
81+
82+
$output .= '</ul>';
83+
$output .= $atts['nonav'] ? '' : '</nav>';
84+
85+
set_transient($cache_key, $output, 12 * HOUR_IN_SECONDS);
86+
} else {
87+
$output = $atts['nonav'] ? '<ul><p>' : '<nav><ul><p>';
88+
$output .= esc_html__('No posts found.', 'relevant-recent-posts');
89+
$output .= $atts['nonav'] ? '</p></ul>' : '</p></ul></nav>';
90+
}
91+
92+
wp_reset_postdata();
93+
}
94+
95+
return $output;
96+
}
97+
98+
add_shortcode('recentposts', 'infinitnet_relevant_recent_posts_shortcode');
99+
}
100+
101+
function infinitnet_clear_recent_relevant_posts_cache($post_id) {
102+
global $wpdb;
103+
104+
$wpdb->query("DELETE FROM $wpdb->options WHERE option_name LIKE '%rrp_{$post_id}_%'");
105+
}
106+
107+
add_action('save_post', 'infinitnet_clear_recent_relevant_posts_cache');
108+
add_action('delete_post', 'infinitnet_clear_recent_relevant_posts_cache');
109+
add_action('transition_post_status', 'infinitnet_clear_recent_relevant_posts_cache', 10, 3);

0 commit comments

Comments
 (0)