No Results Found
The page you requested could not be found. Try refining your search, or use the navigation above to locate the post.
function damalion_get_contextual_recommended_posts(array $args = []) : WP_Query { $defaults = [ 'limit' => 5, 'prefer' => 'tags_then_categories', 'fallback_to_recent' => true, 'post_type' => 'post', ]; $args = array_merge($defaults, $args); $post_id = get_queried_object_id(); $limit = max(1, (int) $args['limit']); // ARCHIVES (/blog/, category, tag, archives): keep current behavior if ( ! is_singular($args['post_type']) ) { if (!(is_home() || is_category() || is_tag() || is_archive())) { return new WP_Query(['post__in' => [0]]); } $q = [ 'post_type' => $args['post_type'], 'posts_per_page' => $limit, 'ignore_sticky_posts' => true, 'no_found_rows' => true, 'orderby' => 'date', 'order' => 'DESC', ]; if (is_category()) { $q['category__in'] = [get_queried_object_id()]; } elseif (is_tag()) { $q['tag__in'] = [get_queried_object_id()]; } return new WP_Query($q); } // SINGLE POST if (!$post_id) { return new WP_Query(['post__in' => [0]]); } $tag_ids = wp_get_post_tags($post_id, ['fields' => 'ids']); $cat_ids = wp_get_post_categories($post_id, ['fields' => 'ids']); // Helper: recent posts fallback $fallback_recent = function() use ($args, $limit, $post_id) { return new WP_Query([ 'post_type' => $args['post_type'], 'posts_per_page' => $limit, 'post__not_in' => [$post_id], 'ignore_sticky_posts' => true, 'no_found_rows' => true, 'orderby' => 'date', 'order' => 'DESC', ]); }; // If no tax at all, fallback if (empty($tag_ids) && empty($cat_ids)) { return !empty($args['fallback_to_recent']) ? $fallback_recent() : new WP_Query(['post__in' => [0]]); } /** * ACCURACY UPGRADE: * 1) Fetch a candidate pool matching ANY shared tag/category * 2) Score candidates (tags weighted heavier than categories) * 3) Return top $limit by score */ $tax_query = ['relation' => 'OR']; if (!empty($tag_ids)) { $tax_query[] = [ 'taxonomy' => 'post_tag', 'field' => 'term_id', 'terms' => $tag_ids, 'operator' => 'IN', ]; } if (!empty($cat_ids)) { $tax_query[] = [ 'taxonomy' => 'category', 'field' => 'term_id', 'terms' => $cat_ids, 'operator' => 'IN', ]; } // Candidate pool (bigger than final limit) $candidate_pool = max(20, $limit * 8); $candidates = get_posts([ 'post_type' => $args['post_type'], 'posts_per_page' => $candidate_pool, 'post__not_in' => [$post_id], 'ignore_sticky_posts' => true, 'no_found_rows' => true, 'fields' => 'ids', 'tax_query' => $tax_query, ]); if (empty($candidates)) { return !empty($args['fallback_to_recent']) ? $fallback_recent() : new WP_Query(['post__in' => [0]]); } // Score each candidate $scores = []; foreach ($candidates as $cid) { $score = 0; if (!empty($tag_ids)) { $ctags = wp_get_post_tags($cid, ['fields' => 'ids']); $shared_tags = array_intersect($tag_ids, $ctags); $score += 3 * count($shared_tags); // tags weight } if (!empty($cat_ids)) { $ccats = wp_get_post_categories($cid, ['fields' => 'ids']); $shared_cats = array_intersect($cat_ids, $ccats); $score += 1 * count($shared_cats); // categories weight } // Tie-breaker: newer content slightly preferred $age_days = max(1, (int) floor((time() - get_post_time('U', true, $cid)) / DAY_IN_SECONDS)); $score += 30 / $age_days; // small boost for newer posts $scores[$cid] = $score; } arsort($scores); // highest score first $top_ids = array_slice(array_keys($scores), 0, $limit); // Return WP_Query preserving our chosen order return new WP_Query([ 'post_type' => $args['post_type'], 'posts_per_page' => $limit, 'post__in' => $top_ids, 'orderby' => 'post__in', 'ignore_sticky_posts' => true, 'no_found_rows' => true, ]); }
The page you requested could not be found. Try refining your search, or use the navigation above to locate the post.