diff --git a/includes/functions/_api.php b/includes/functions/_api.php index 8dcf2ed8..b3830abd 100644 --- a/includes/functions/_api.php +++ b/includes/functions/_api.php @@ -109,7 +109,7 @@ if ( ! function_exists( 'fictioneer_api_get_story_node' ) ) { array( 'post_type' => 'fcn_chapter', 'post_status' => 'publish', - 'post__in' => $data['chapter_ids'], + 'post__in' => fictioneer_post__in( $data['chapter_ids'] ), 'ignore_sticky_posts' => true, 'orderby' => 'post__in', 'posts_per_page' => -1, diff --git a/includes/functions/_content_helpers.php b/includes/functions/_content_helpers.php index 14fe8778..9741c231 100644 --- a/includes/functions/_content_helpers.php +++ b/includes/functions/_content_helpers.php @@ -878,7 +878,7 @@ if ( ! function_exists( 'fictioneer_get_chapter_list_items' ) ) { $hide_icons = fictioneer_get_field( 'fictioneer_story_hide_chapter_icons', $story_id ) || get_option( 'fictioneer_hide_chapter_icons' ); $query_args = array( - 'post__in' => $data['chapter_ids'], // Only visible and published + 'post__in' => fictioneer_post__in( $data['chapter_ids'] ), // Only visible and published 'post_type' => 'fcn_chapter', 'orderby' => 'post__in', 'posts_per_page' => -1, diff --git a/includes/functions/_epub.php b/includes/functions/_epub.php index d90a3243..e915ae4f 100644 --- a/includes/functions/_epub.php +++ b/includes/functions/_epub.php @@ -305,7 +305,7 @@ if ( ! function_exists( 'fictioneer_add_epub_chapters' ) ) { $query_args = array( 'post_type' => 'fcn_chapter', 'post_status' => 'publish', - 'post__in' => $chapters, + 'post__in' => fictioneer_post__in( $chapters ), 'orderby' => 'post__in', 'posts_per_page' => -1, 'update_post_term_cache' => false, // Improve performance diff --git a/includes/functions/_shortcodes.php b/includes/functions/_shortcodes.php index d5fcad0b..8d4ff191 100644 --- a/includes/functions/_shortcodes.php +++ b/includes/functions/_shortcodes.php @@ -981,7 +981,7 @@ function fictioneer_shortcode_chapter_list( $attr ) { $query_args = array( 'post_type' => 'fcn_chapter', 'post_status' => 'publish', - 'post__in' => $chapters, + 'post__in' => fictioneer_post__in( $chapters ), 'ignore_sticky_posts' => true, 'orderby' => 'post__in', // Preserve order from meta box 'posts_per_page' => -1, // Get all chapters (this can be hundreds) diff --git a/includes/functions/_utility.php b/includes/functions/_utility.php index 34140a06..736a3080 100644 --- a/includes/functions/_utility.php +++ b/includes/functions/_utility.php @@ -176,7 +176,7 @@ if ( ! function_exists( 'fictioneer_get_story_data' ) ) { array( 'status' => 'approve', 'post_type' => 'fcn_chapter', - 'post__in' => $old_data['chapter_ids'], + 'post__in' => fictioneer_post__in( $old_data['chapter_ids'] ), 'count' => true, 'update_comment_meta_cache' => false ) @@ -228,11 +228,11 @@ if ( ! function_exists( 'fictioneer_get_story_data' ) ) { } // Query chapters - $chapters = new WP_Query( + $chapters = empty( $chapters ) ? $chapters : new WP_Query( array( 'post_type' => 'fcn_chapter', 'post_status' => 'publish', - 'post__in' => $chapters, + 'post__in' => fictioneer_post__in( $chapters ), 'ignore_sticky_posts' => true, 'orderby' => 'post__in', // Preserve order from meta box 'posts_per_page' => -1, // Get all chapters (this can be hundreds) @@ -241,7 +241,7 @@ if ( ! function_exists( 'fictioneer_get_story_data' ) ) { ); // Count chapters and words - if ( $chapters->have_posts() ) { + if ( ! empty( $chapters ) && $chapters->have_posts() ) { foreach ( $chapters->posts as $chapter ) { // This is about 50 times faster than using a meta query lol if ( ! fictioneer_get_field( 'fictioneer_chapter_hidden', $chapter->ID ) ) { @@ -260,7 +260,7 @@ if ( ! function_exists( 'fictioneer_get_story_data' ) ) { $comment_args = array( 'status' => 'approve', 'post_type' => 'fcn_chapter', - 'post__in' => $chapter_ids, + 'post__in' => fictioneer_post__in( $chapter_ids ), 'count' => true, 'update_comment_meta_cache' => false ); @@ -1739,4 +1739,25 @@ function fictioneer_delete_my_account() { return false; } +// ============================================================================= +// POST__IN SAFETY CHECK +// ============================================================================= + +/** + * Returns the given array or an array with a single zero element if empty + * + * Prevents an array from being empty, which is useful for 'post__in' query + * arguments that cannot deal with empty arrays. + * + * @since Fictioneer 5.6.0 + * + * @param array $array The input array to check. + * + * @return array The original array or [0]. + */ + +function fictioneer_post__in( $array ) { + return empty( $array ) ? [0] : $array; +} + ?> diff --git a/includes/functions/comments/_story_comments.php b/includes/functions/comments/_story_comments.php index 5376edc5..bdbbd626 100644 --- a/includes/functions/comments/_story_comments.php +++ b/includes/functions/comments/_story_comments.php @@ -122,7 +122,7 @@ function fictioneer_request_story_comments() { array( 'status' => 'approve', 'post_type' => ['fcn_chapter'], - 'post__in' => $chapter_ids, + 'post__in' => fictioneer_post__in( $chapter_ids ), 'number' => $comments_per_page, 'paged' => $page, 'meta_query' => array( diff --git a/includes/functions/hooks/_post_hooks.php b/includes/functions/hooks/_post_hooks.php index 8faf81aa..17c9f726 100644 --- a/includes/functions/hooks/_post_hooks.php +++ b/includes/functions/hooks/_post_hooks.php @@ -55,7 +55,7 @@ function fictioneer_post_featured_list( $post_id ) { $query_args = array( 'post_type' => 'any', 'post_status' => 'publish', - 'post__in' => $featured, + 'post__in' => fictioneer_post__in( $featured ), 'orderby' => 'post__in', 'posts_per_page' => -1, 'no_found_rows' => true // Improve performance diff --git a/includes/functions/users/_checkmarks.php b/includes/functions/users/_checkmarks.php index 1c9210df..7b511453 100644 --- a/includes/functions/users/_checkmarks.php +++ b/includes/functions/users/_checkmarks.php @@ -258,7 +258,7 @@ function fictioneer_ajax_get_finished_list() { $list_items = fictioneer_get_card_list( 'story', array( - 'post__in' => $post_ids, + 'post__in' => fictioneer_post__in( $post_ids ), 'paged' => $page, 'order' => $order ), diff --git a/includes/functions/users/_follows.php b/includes/functions/users/_follows.php index c7fd831d..84f454be 100644 --- a/includes/functions/users/_follows.php +++ b/includes/functions/users/_follows.php @@ -417,7 +417,7 @@ function fictioneer_ajax_get_follows_list() { $list_items = fictioneer_get_card_list( 'story', array( - 'post__in' => $post_ids, + 'post__in' => fictioneer_post__in( $post_ids ), 'paged' => $page, 'order' => $order ), diff --git a/includes/functions/users/_reminders.php b/includes/functions/users/_reminders.php index 080edeb8..94750f28 100644 --- a/includes/functions/users/_reminders.php +++ b/includes/functions/users/_reminders.php @@ -187,7 +187,7 @@ function fictioneer_ajax_get_reminders_list() { $list_items = fictioneer_get_card_list( 'story', array( - 'post__in' => $post_ids, + 'post__in' => fictioneer_post__in( $post_ids ), 'paged' => $page, 'order' => $order ), diff --git a/partials/_card-collection.php b/partials/_card-collection.php index 1d50c662..b2dbf11f 100644 --- a/partials/_card-collection.php +++ b/partials/_card-collection.php @@ -55,7 +55,7 @@ if ( ! empty( $items ) ) { array( 'post_type' => 'any', 'post_status' => 'publish', - 'post__in' => $items, + 'post__in' => fictioneer_post__in( $items ), 'ignore_sticky_posts' => true, 'orderby' => 'modified', 'posts_per_page' => -1, @@ -95,7 +95,7 @@ if ( ! empty( $items ) ) { $chapter_query_args = array( 'post_type' => 'fcn_chapter', 'post_status' => 'publish', - 'post__in' => $chapter_ids, + 'post__in' => fictioneer_post__in( $chapter_ids ), 'posts_per_page' => -1, 'update_post_term_cache' => false, // Improve performance 'no_found_rows' => true // Improve performance @@ -129,7 +129,7 @@ if ( ! empty( $items ) ) { $comment_args = array( 'post_type' => 'fcn_chapter', 'status' => 1, - 'post__in' => $processed_ids, + 'post__in' => fictioneer_post__in( $processed_ids ), 'count' => true, 'update_comment_meta_cache' => false // Improve performance ); diff --git a/partials/_card-story.php b/partials/_card-story.php index 02aadced..c4724617 100644 --- a/partials/_card-story.php +++ b/partials/_card-story.php @@ -102,7 +102,7 @@ $is_sticky = FICTIONEER_ENABLE_STICKY_CARDS && $chapter_query_args = array( 'post_type' => 'fcn_chapter', 'post_status' => 'publish', - 'post__in' => $chapter_ids, + 'post__in' => fictioneer_post__in( $chapter_ids ), 'posts_per_page' => -1, 'no_found_rows' => true, // Improve performance 'update_post_term_cache' => false // Improve performance diff --git a/partials/_collection-statistics.php b/partials/_collection-statistics.php index 0d33983e..0ef5081d 100644 --- a/partials/_collection-statistics.php +++ b/partials/_collection-statistics.php @@ -55,7 +55,7 @@ if ( ! empty( $args['featured_list'] ) ) { $chapter_query_args = array( 'post_type' => 'fcn_chapter', 'post_status' => 'publish', - 'post__in' => $query_chapter_ids, + 'post__in' => fictioneer_post__in( $query_chapter_ids ), 'posts_per_page' => -1, 'update_post_term_cache' => false // Improve performance ); @@ -98,7 +98,7 @@ if ( ! empty( $args['featured_list'] ) ) { get_comments( array( 'post_type' => 'fcn_chapter', - 'post__in' => $query_chapter_ids, + 'post__in' => fictioneer_post__in( $query_chapter_ids ), 'status' => 1, 'count' => true, 'update_comment_meta_cache' => false diff --git a/partials/_latest-chapters-compact.php b/partials/_latest-chapters-compact.php index b1518c7e..1ba05e82 100644 --- a/partials/_latest-chapters-compact.php +++ b/partials/_latest-chapters-compact.php @@ -29,7 +29,7 @@ $query_args = array( 'post_type' => 'fcn_chapter', 'post_status' => 'publish', - 'post__in' => $args['post_ids'], + 'post__in' => fictioneer_post__in( $args['post_ids'] ), 'order' => $args['order'] ?? 'desc', 'orderby' => $args['orderby'] ?? 'date', 'posts_per_page' => $args['count'], diff --git a/partials/_latest-chapters.php b/partials/_latest-chapters.php index 861e344e..c5b85fd2 100644 --- a/partials/_latest-chapters.php +++ b/partials/_latest-chapters.php @@ -30,7 +30,7 @@ $query_args = array( 'post_type' => 'fcn_chapter', 'post_status' => 'publish', - 'post__in' => $args['post_ids'], + 'post__in' => fictioneer_post__in( $args['post_ids'] ), 'orderby' => $args['orderby'] ?? 'date', 'order' => $args['order'] ?? 'desc', 'posts_per_page' => $args['count'], diff --git a/partials/_latest-posts.php b/partials/_latest-posts.php index aac2f50e..4de3f7a0 100644 --- a/partials/_latest-posts.php +++ b/partials/_latest-posts.php @@ -25,7 +25,7 @@ $query_args = array( 'post_type' => 'post', 'post_status' => 'publish', - 'post__in' => $args['post_ids'], + 'post__in' => fictioneer_post__in( $args['post_ids'] ), 'has_password' => false, 'orderby' => 'date', 'order' => 'DESC', diff --git a/partials/_latest-recommendations-compact.php b/partials/_latest-recommendations-compact.php index 50aa9b28..ba1e53a1 100644 --- a/partials/_latest-recommendations-compact.php +++ b/partials/_latest-recommendations-compact.php @@ -29,7 +29,7 @@ $show_taxonomies = ! get_option( 'fictioneer_hide_taxonomies_on_recommendation_c $query_args = array ( 'post_type' => 'fcn_recommendation', 'post_status' => 'publish', - 'post__in' => $args['post_ids'], + 'post__in' => fictioneer_post__in( $args['post_ids'] ), 'orderby' => $args['orderby'] ?? 'date', 'order' => $args['order'] ?? 'desc', 'posts_per_page' => $args['count'], diff --git a/partials/_latest-recommendations.php b/partials/_latest-recommendations.php index 76530278..ba5be67f 100644 --- a/partials/_latest-recommendations.php +++ b/partials/_latest-recommendations.php @@ -30,7 +30,7 @@ $show_taxonomies = ! get_option( 'fictioneer_hide_taxonomies_on_recommendation_c $query_args = array ( 'post_type' => 'fcn_recommendation', 'post_status' => 'publish', - 'post__in' => $args['post_ids'], + 'post__in' => fictioneer_post__in( $args['post_ids'] ), 'orderby' => $args['orderby'] ?? 'date', 'order' => $args['order'] ?? 'desc', 'posts_per_page' => $args['count'], diff --git a/partials/_latest-stories-compact.php b/partials/_latest-stories-compact.php index 7bdc261e..96d244da 100644 --- a/partials/_latest-stories-compact.php +++ b/partials/_latest-stories-compact.php @@ -30,7 +30,7 @@ $show_taxonomies = ! get_option( 'fictioneer_hide_taxonomies_on_story_cards' ); $query_args = array( 'post_type' => 'fcn_story', 'post_status' => 'publish', - 'post__in' => $args['post_ids'], + 'post__in' => fictioneer_post__in( $args['post_ids'] ), 'orderby' => $args['orderby'], 'order' => $args['order'] ?? 'desc', 'posts_per_page' => $args['count'], diff --git a/partials/_latest-stories.php b/partials/_latest-stories.php index 3df3ca26..1c2bd518 100644 --- a/partials/_latest-stories.php +++ b/partials/_latest-stories.php @@ -27,7 +27,7 @@ $query_args = array( 'post_type' => 'fcn_story', 'post_status' => 'publish', - 'post__in' => $args['post_ids'], + 'post__in' => fictioneer_post__in( $args['post_ids'] ), 'orderby' => $args['orderby'], 'order' => $args['order'] ?? 'desc', 'posts_per_page' => $args['count'], diff --git a/partials/_latest-updates-compact.php b/partials/_latest-updates-compact.php index 499a4a00..ae23839b 100644 --- a/partials/_latest-updates-compact.php +++ b/partials/_latest-updates-compact.php @@ -33,7 +33,7 @@ $card_counter = 0; $query_args = array( 'post_type' => 'fcn_story', 'post_status' => 'publish', - 'post__in' => $args['post_ids'], + 'post__in' => fictioneer_post__in( $args['post_ids'] ), 'meta_key' => 'fictioneer_chapters_added', 'orderby' => 'meta_value', 'order' => $args['order'] ?? 'desc', diff --git a/partials/_latest-updates.php b/partials/_latest-updates.php index 7393e1b9..ac216bc0 100644 --- a/partials/_latest-updates.php +++ b/partials/_latest-updates.php @@ -35,7 +35,7 @@ $card_counter = 0; $query_args = array( 'post_type' => 'fcn_story', 'post_status' => 'publish', - 'post__in' => $args['post_ids'], + 'post__in' => fictioneer_post__in( $args['post_ids'] ), 'meta_key' => 'fictioneer_chapters_added', 'orderby' => 'meta_value', 'order' => $args['order'] ?? 'desc', diff --git a/partials/_showcase.php b/partials/_showcase.php index 263f7551..731c5029 100644 --- a/partials/_showcase.php +++ b/partials/_showcase.php @@ -31,7 +31,7 @@ $query_args = array ( 'post_type' => $args['type'], 'post_status' => 'publish', - 'post__in' => $args['post_ids'], + 'post__in' => fictioneer_post__in( $args['post_ids'] ), 'order' => $args['order'] ?? 'DESC', 'orderby' => $args['orderby'] ?? 'date', 'posts_per_page' => $args['count'] ?? 8, diff --git a/partials/_story-content.php b/partials/_story-content.php index e2c16082..4b723006 100644 --- a/partials/_story-content.php +++ b/partials/_story-content.php @@ -143,7 +143,7 @@ $disable_folding = fictioneer_get_field( 'fictioneer_story_disable_collapse' ); array( 'post_type' => 'fcn_chapter', 'post_status' => 'publish', - 'post__in' => $chapters, + 'post__in' => fictioneer_post__in( $chapters ), 'ignore_sticky_posts' => true, 'orderby' => 'post__in', // Preserve order from meta box 'posts_per_page' => -1, // Get all chapters (this can be hundreds) diff --git a/rss-rss-story.php b/rss-rss-story.php index 00262f00..e283b98b 100644 --- a/rss-rss-story.php +++ b/rss-rss-story.php @@ -124,7 +124,7 @@ do_action( 'rss_tag_pre', 'rss2' ); ?> 'any', 'post_status' => 'publish', - 'post__in' => $featured_list, + 'post__in' => fictioneer_post__in( $featured_list ), 'ignore_sticky_posts' => 1, 'posts_per_page' => -1, 'no_found_rows' => true // Improve performance @@ -63,7 +63,7 @@ // Prepare paginated featured query $query_args = array ( 'post_type' => 'any', - 'post__in' => $featured_list, + 'post__in' => fictioneer_post__in( $featured_list ), 'ignore_sticky_posts' => 1, 'orderby' => 'modified', 'order' => 'DESC', diff --git a/singular-bookshelf.php b/singular-bookshelf.php index 106cd022..61612846 100644 --- a/singular-bookshelf.php +++ b/singular-bookshelf.php @@ -164,7 +164,7 @@ $tabs[ $current_tab ]['classes'][] = '_current'; $list_items = fictioneer_get_card_list( 'story', array( - 'post__in' => $tabs[ $current_tab ]['post_ids'], + 'post__in' => fictioneer_post__in( $tabs[ $current_tab ]['post_ids'] ), 'paged' => $current_page, 'order' => $order ),