WordPress Relevanssi插件:提升标题含特定词页面搜索排名求助
Fixing Your Relevanssi Title Boost for "Courses" Pages
Hey there! Let's break down what's going wrong with your modified code and fix it step by step—since you're new to PHP and WordPress, I'll keep things clear and straightforward.
What's Wrong with Your Current Code?
You made a few key mistakes that are preventing the function from working:
- Incorrect Post Data Retrieval:
relevanssi_get_the_title()only returns a plain text string of the post title, not a full post object. So when you try to access$post->post_content, you're trying to get a property from a string—which throws an error. - Undefined Variable & Broken Logic: You used
$post_titlewithout defining it first, and thestrpos(strrpos(...))syntax is mixed up. This part doesn't actually check if the title contains "courses" like you intended. - Invalid Weight Assignment: The way you're trying to update the result weight (
$results[$post_title->strpos(...)]) doesn't target the correct post ID, so Relevanssi can't apply the boost properly.
Corrected Code
Here's a working version of your function that will boost any page where the title contains "courses" (case-insensitive):
/* Boost pages with "courses" in the title in Relevanssi search results */ add_filter('relevanssi_results', 'course_title_boost'); function course_title_boost($results) { // Get the current search query (lowercase for consistent checks) $query = strtolower(get_search_query()); // Loop through each result in Relevanssi's results array foreach ($results as $post_id => $weight) { // Fetch the full post object using Relevanssi's helper function $post = relevanssi_get_post($post_id); // Check if the post title contains "courses" (case-insensitive) // stripos() returns the position of the match, or false if not found if (stripos($post->post_title, 'courses') !== false) { // Multiply the existing weight by 200 to boost the ranking // Adjust this number (200) to make the boost stronger/weaker $results[$post_id] = $weight * 200; } // Optional: Add extra boost if the search query also matches the title // Uncomment this block if you want double the boost for exact query matches + "courses" in title // if (stripos($post->post_title, 'courses') !== false && stripos($post->post_title, $query) !== false) { // $results[$post_id] = $weight * 300; // } } return $results; }
Key Notes for Implementation
- Where to Add This Code: Paste this into your active theme's
functions.phpfile, or use a custom code plugin like Code Snippets (never edit plugin files directly—your changes will be lost when plugins update). - Adjust the Boost Multiplier: The
200value controls how much the ranking is boosted. If you want a smaller boost, use a lower number (like5); for a bigger push, use a higher number (like500). - Reindex Relevanssi: After adding the code, go to Relevanssi settings → "Indexing" and click "Rebuild Index" to make sure the new weighting logic is applied to all posts.
内容的提问来源于stack exchange,提问作者Thoanga
相关产品推荐
相关产品推荐

