From 36aac5bd3e2072ece2754bc16840f2bcd31320c8 Mon Sep 17 00:00:00 2001 From: Tetrakern <26898880+Tetrakern@users.noreply.github.com> Date: Tue, 7 Jan 2025 11:34:40 +0100 Subject: [PATCH] Add filters for RSS query args --- FILTERS.md | 37 +++++++++++++++++++++++++++++++++++++ rss-rss-main.php | 31 +++++++++++++++++-------------- rss-rss-story.php | 3 +++ 3 files changed, 57 insertions(+), 14 deletions(-) diff --git a/FILTERS.md b/FILTERS.md index 85f8112f..531b677b 100644 --- a/FILTERS.md +++ b/FILTERS.md @@ -1421,6 +1421,43 @@ Filters the RSS link returned by the `fictioneer_get_rss_link( $post_type, $post --- +### `apply_filters( 'fictioneer_filter_rss_main_query_args', $query_args )` +Filters the query arguments for the main RSS feed in the `rss-rss-main.php` template. + +**Parameters:** +* $query_args (array) – Associative array of query arguments. + +**Example:** +```php +function child_exclude_protected_posts_from_main_rss( $query_args ) { + $query_args['has_password'] = false; + + return $query_args; +} +add_filter( 'fictioneer_filter_rss_story_query_args', 'child_exclude_protected_posts_from_main_rss' ); +``` + +--- + +### `apply_filters( 'fictioneer_filter_rss_story_query_args', $query_args, $story_id )` +Filters the query arguments for the story RSS feed in the `rss-rss-story.php` template. + +**Parameters:** +* $query_args (array) – Associative array of query arguments. +* $story_id (int) – Post ID of the story. + +**Example:** +```php +function child_exclude_protected_posts_from_story_rss( $query_args ) { + $query_args['has_password'] = false; + + return $query_args; +} +add_filter( 'fictioneer_filter_rss_story_query_args', 'child_exclude_protected_posts_from_story_rss' ); +``` + +--- + ### `apply_filters( 'fictioneer_filter_safe_title', $title, $post_id, $context, $args )` Filters the string returned by the `fictioneer_get_safe_title( $post_id )` function, after all tags and line breaks have been stripped. No further sanitization is applied here, so you can add HTML again. diff --git a/rss-rss-main.php b/rss-rss-main.php index 92d4cce7..2fbd03c1 100644 --- a/rss-rss-main.php +++ b/rss-rss-main.php @@ -8,22 +8,25 @@ * @link https://github.com/WordPress/WordPress/blob/master/wp-includes/feed-rss2.php */ -// Query posts -$posts = get_posts( - array( - 'fictioneer_query_name' => 'main_rss', - 'post_type' => ['post', 'fcn_story', 'fcn_chapter', 'fcn_recommendation'], - 'post_status' => 'publish', - 'ignore_sticky_posts' => 1, - 'orderby' => 'date', - 'order' => 'DESC', - 'posts_per_page' => get_option( 'posts_per_rss' ) + 8, // Buffer in case of invalid results - 'update_post_meta_cache' => true, - 'update_post_term_cache' => true, - 'no_found_rows' => true - ) + +// Query +$query_args = array( + 'fictioneer_query_name' => 'main_rss', + 'post_type' => ['post', 'fcn_story', 'fcn_chapter', 'fcn_recommendation'], + 'post_status' => 'publish', + 'ignore_sticky_posts' => 1, + 'orderby' => 'date', + 'order' => 'DESC', + 'posts_per_page' => get_option( 'posts_per_rss' ) + 8, // Buffer in case of invalid results + 'update_post_meta_cache' => true, + 'update_post_term_cache' => true, + 'no_found_rows' => true // Improve performance ); +$query_args = apply_filters( 'fictioneer_filter_rss_main_query_args', $query_args ); + +$posts = get_posts( $query_args ); + // Filter out hidden posts (faster than meta query) $posts = array_filter( $posts, function ( $post ) { // Chapter hidden? diff --git a/rss-rss-story.php b/rss-rss-story.php index 803b559a..720996f3 100644 --- a/rss-rss-story.php +++ b/rss-rss-story.php @@ -13,6 +13,7 @@ * @link https://github.com/WordPress/WordPress/blob/master/wp-includes/feed-rss2.php */ + // Get ID from parameter $story_id = fictioneer_validate_id( $_GET[ 'story_id' ] ?? 0, 'fcn_story' ); $is_hidden = get_post_meta( $story_id ?: 0, 'fictioneer_story_hidden', true ); @@ -142,6 +143,8 @@ do_action( 'rss_tag_pre', 'rss2' ); 'no_found_rows' => true // Improve performance ); + $query_args = apply_filters( 'fictioneer_filter_rss_story_query_args', $query_args, $story_id ); + $chapter_query = new WP_Query( $query_args ); // Loop over chapters